DOM Flashcards

1
Q

div.id = i

A

event.target

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

header. classList.add(‘header’)
header. classList.remove(‘header’)
header. classList.contains(“header”)

A
//add a classname to the div
div.className = "box"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

The syntax follows an element.style.property format, with the property representing a CSS property. For example, the following code selects the first element with a class of blue and assigns blue as the background-color:

let blueElement = document.querySelector('.blue');
blueElement.style.backgroundColor = 'blue';
A

document.querySelector(‘.blue’).style.fontFamily = ‘Roboto’;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

// style the <p> element

para.style.border = ‘1px solid black’
</p>

A

// header styling (not recommended this way)

header. style.width = ‘100%’
header. style.height = ‘100px’
header. style.background = ‘lightBlue’
header. textContent = ‘This is the menu’

header. style.display = ‘flex’
header. style.justifyContent = ‘center’
header. style.alignItems = ‘center’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

// create a new element

const header = document.createElement(‘header’);

A

const body = document.querySelector(‘body’);

The querySelector() method returns the first element that matches a CSS selector.

To return all matches (not only the first), use the querySelectorAll() instead.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

// add the element to the dom

body.appendChild(header)

A
//select  by classname
const div = document.querySelector('.test') 

console.log(‘div is’, div)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

//Create and Insert Elements

let paragraph = document.createElement(‘p’);

The .createElement() method creates a new element based on the specified tag name passed into it as an argument. In the example code above, the .createElement() method takes ‘p’ as its argument which creates an empty <p> element and stores it as the paragraph variable.

</p>

A

//Assign a value to the element

paragraph. id = ‘info’;
paragraph. innerHTML = ‘The text inside the paragraph’;

Above, we use the .id property to assign ‘info’ as ID and the .innerHTML property to set ‘The text inside the paragraph’ as the content of the <p> element.</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
// select an element by ID
const divById = document.getElementById('a1')
console.log('divbyid is', divById)
A

// create an element

const para = document.createElement(‘p’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

// add a <p> element to the </p><div> (parent)

div.appendChild(para)
</div>

A
//add a text(string) to the <p> element
 para.innerText = 'Hello from JS'</p>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
//tweak the body element
document.body.innerHTML = 'The cat loves the dog.';

//tweak the <h2> element

document.body.innerHTML = ‘</h2><h2>This is a heading</h2>’;

A

//select and modify elements

The .querySelector() method allows us to specify a CSS selector as a string and returns the first element that matches that selector.

document.querySelector(‘p’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

// access an elemetn by ID

document.getElementById(‘bio’).innerHTML = ‘The description’;

A

.getElementsByClassName()

document.getElementsByClassName(‘student’)[0].innerHTML = ‘Not yet registered’;

methods that return an array of elements, instead of just one element. You can use bracket notation to access individual elements of an array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

The .appendChild() method will add a child element as the parent element’s last child node. The following code appends the <p> element stored in the paragraph variable to the document body.

document.body.appendChild(paragraph);

The .appendChild() method does not replace the content inside of the parent, in this case, body. Rather, it appends the new element as the last child of that parent.</p>

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly