jQuery Fundamentals Flashcards
How does the for each function work with a set of jquery elements?
$someEls.each( function( index, element ) { // act on els } );
What is the difference between $(element) and $(this) in a jquery().each() loop?
they’re the same
What’s the difference between el.append() and el.after()
append treats ‘el’ as parent, and puts something inside it at the end. After simply puts something after the element.
What jQuery method sets and overrides default options in a plugin?
$.extend( { key1: defaultVal1, key2: defaultVal2 }, overrides ); // the overrides var would be passed into the plugin
Will this work? $.extend( { someObj { }, someKey: val }, overrides } );
No, because extend doesn’t do a deep merge. Add “true” to make it work.
How does $.each() work?
It takes an array or an object as the first argument and a callback as the second. Callback: function( key, objValProp) {} or else function ( index, objValue ) depending on object or array.
Search for class “box” inside of the #main section using context parameter.
$( “.box”, “#main”);
Get a set of all paragraph elements inside div id=”main” as well as the div. That is, return a set of p’s and div#main.
$(“#main”).find(“p”).andSelf(); // or $(“p”, “#main”).andSelf()
Get or set an attribute like id or src
$(“#someElement”).attr( “id”, “newID” ); // set
$(“#el”).attr( “id” ); // get
Set multiple attributes at once on an element
$( “el” ).attr( { “id”: “newID”, “title”: “newTitle” } );
What’s the difference between $( ‘p’ ).html() and $( ‘p’ ).text() ?
html() returns all the unescaped characters and attributes (e.g for bold text or spans). Text() only returns the text.
What’s the difference between $( ‘p’ ).html() and $( ‘p’ ).text() ?
html() returns all the unescaped characters and attributes (e.g for bold text or spans). Text() only returns the text.
Fastest way to select all direct children of a node?
$( someNode ).children(); // faster than $(“ node > * “);
What’s main difference between $(“div”).find(“.someClass”) and .filter( “.someClass” );
Find looks to all the children of div collection and filter looks to only the div collection
What’s main difference between $(“div”).find(“.someClass”) and .filter( “.someClass” );
Find looks to all the children of div collection and filter looks to only the div collection