Essentials Flashcards

1
Q

Get element collection

var elements = $(‘div’);

A

var elements = document.queryAll(‘div’);

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

Get element collection in a specific container

var elements = $(‘div’, container);

A

var elements = container.queryAll(‘div’);

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

Create new element

var element = $(‘div’);

A

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

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

Execute function when document is fully loaded –document.ready

$(function() {

console.log( “ready!” );

});

or

$( document ).ready(function() {

console.log( “ready!” );

});

A

document. addEventListener(‘DOMContentLoaded’, function() {
console. log( “ready!” );

});

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

Each – Iterate over a jQuery object, executing a function for each matched element.

elements.each(function() {

$( this ).addClass( “foo” );

});

$( “li” ).each(function() {

$( this ).addClass( “foo” );

});

A

ES6

for (var element of elements) element.doSomething();

ES5

elements.forEach(function(element) { element.doSomething();

});

For large collections use

for (var id = 0; id < elements.length; id++) elements[id].doSomething();

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