vanilla JS Flashcards

1
Q

Select DOM elements by CSS selector

A
var matches = 
// elements with  a div class="foo"
document.querySelectorAll('div.foo');

for (i=0; i

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

Select elements by class name

A

var list = document.getElementsByClassName(‘foo’);

	// get the number of selected elements
	console.log(list.length);
	// iterate over elements and output their HTML content
for (var i=0; i
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Select elements by tag name

A
var list = 
// METHOD 1
document.getElementsByTagName('a');
	// get the number of selected elements
	console.log(list.length);
	// iterate over elements and output href attribute values
	for (var i=0; i
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Select an element by ID

A
var el = document.getElementById('foo');
	el.innerHTML = 'Hello World!';
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Get parent element node

A
var el = document.querySelector('div');
	var parent = el.parentNode;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Get siblings of an element

A
// METHOD 1
function getSiblings(el, filter) {
	    var siblings = [];
	    el = el.parentNode.firstChild;
	    do { if (!filter || filter(el)) siblings.push(el); } while (el = el.nextSibling);
	    return siblings;
	}
	// example filter function
	function exampleFilter(el) {
	    return elem.nodeName.toLowerCase() == 'a';
	}
	// usage
	el = document.querySelector('div');
	// get all siblings of el
	var sibs = getSiblings(el);
	// get only anchor element siblings of el
	var sibs_a = getSiblings(el, exampleFilter);
// METHOD 2
var previous = el.previousSibling;

var next = el.nextSibling;

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

Getting the children of a DOM element.

A

var el = document.querySelector(‘div’);

	var children = el.childNodes,
	    number_of_children = children.length;
for (var i=0; i
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Create a DOM element

A

var el = document.createElement(‘div’);

el.innerHTML = ‘<p>Hello World!</p>’;

// alternatively,
var textnode = document.createTextNode('Hello World!');
	el.appendChild(textnode);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Replace a DOM element

A

var el = document.querySelector(‘div’);

	// <a href="/javascript/manipulation/creating-a-dom-element-51/">create a new element</a> that will take the place of "el"
	var newEl = document.createElement('p');
	newEl.innerHTML = '<b>Hello World!</b>';
	// replace el with newEL
	el.parentNode.replaceChild(newEl, el);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Unwrap a DOM element

Remove the parents of an element from the DOM, leaving the element’s content in place.

A
// select element to unwrap
	var el = document.querySelector('div');
	// get the element's parent node
	var parent = el.parentNode;
	// move all children out of the element
	while (el.firstChild) parent.insertBefore(el.firstChild, el);
	// remove the empty element
	parent.removeChild(el);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Empty an element’s content

Remove all child nodes of an element from the DOM.

A
var el = document.querySelector('div');
	el.innerHTML = '';
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Removing an element

A
var el = document.querySelector('div'); // select the first returned <div> element 
	el.parentNode.removeChild(el);</div>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Insert an element after or before another

A
function insertAfter(el, referenceNode) {
	    referenceNode.parentNode.insertBefore(el, referenceNode.nextSibling);
	}
	// example
	var newEl = document.createElement('div');
	newEl.innerHTML = '<p>Hello World!</p>';
var ref = document.querySelector('div.before');

insertAfter(newEl, ref);
// insert before
function insertBefore(el, referenceNode) {
	    referenceNode.parentNode.insertBefore(el, referenceNode);
	}
	// example
	var newEl = document.createElement('div');
	newEl.innerHTML = '<p>Hello World!</p>';
var ref = document.querySelector('div.before');

insertBefore(newEl, ref);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Get the text content of an element

Get the combined text contents of an element, including its descendants, or set the text content of the element.

A
var el = document.querySelector('div');
	text = el.textContent || el.innerText;
	console.log(text);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Get and set the HTML content of an element

Read or write the HTML content of an element.

A

var el = document.querySelector(‘div’);

	// get HTML content
	console.log(el.innerHTML);
	// set HTML content
	el.innerHTML = '<p>Hello World!</p>'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Append or prepend to an element

Insert a new element or an HTML structure to the end or the beginning of another element’s content.

A

var el = document.querySelector(‘div’);

	// get element content as string
	console.log(el.innerHTML)
	// append to the element's content
	el.innerHTML += '<p>Hello World!</p>';
	// prepend to the element's content
	el.innerHTML = '<p>Hello World!</p>' + el.innerHTML;
17
Q

Clone an element

A

var el = document.querySelector(‘div’);

var foo = el.cloneNode(true);
18
Q

Iterating over a list of selected elements

A

var divs = document.querySelectorAll(‘div’), len = divs.length;

for (var i=0; i

19
Q

Set, get, and remove properties of DOM elements

A
var el = document.querySelector('a');
	console.log(el.href);
	if (el.title != 'foo') el.title = 'foo';
var inp = document.querySelector('input[type="text"]');

console. log(inp.value);
inp. value = 'Hello World!';

var el = document.querySelector('div');

el. foo = { bar: true };
console. log(el.foo);