Essentials Flashcards
Get element collection
var elements = $(‘div’);
var elements = document.queryAll(‘div’);
Get element collection in a specific container
var elements = $(‘div’, container);
var elements = container.queryAll(‘div’);
Create new element
var element = $(‘div’);
var element = document.createElement(‘div’);
Execute function when document is fully loaded –document.ready
$(function() {
console.log( “ready!” );
});
or
$( document ).ready(function() {
console.log( “ready!” );
});
document. addEventListener(‘DOMContentLoaded’, function() {
console. log( “ready!” );
});
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” );
});
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();