jQuery 1 Flashcards
Under what circumstances is it not necessary to use document.ready()?
If you place code just before closing body tag, you don’t need it — the result will be the same.
You want to find all cases where class = “special” within a div with id = “container”; what are two ways to do this?
- Use context parameter: $(“.special”, “#container”);
2. Use find(): $(“#container”).find(“.special”);
What can the filter() method do that find() or just plain old $(“.someClass”) can’t do?
- It can find things that aren’t in the DOM (e.g. in a documentFragment).
- It can take a function which takes one argument, index.
What does end() do?
It reverts the selection to its prior state. $(‘p’).find(‘.someClass”)..perform some actions on someClass set….end()…perform some actions on set of p tags.
Find an element in a given set by index (integer)
Use the eq(i) function: $(“li”).eq(1);
Alt: $(“li:eq(1)); // NOT AS FAST
Name 10 DOM manipulation methods
append(), prepend(), prependTo(), after(), before(), insertAfter(), insertBefore(), wrap(), wrapAll(), wrapInner()
What are two ways to select the direct descendants (children) of ul that are li elements?
$(“ul”).children(“li”) or $(“ul > li”);
What are the three kinds of selector filters?
Basic, Child, and Content Filter. Basic: things like :eq, :first, :lt, :not Child: :first-child, :nth-of-type, etc Content: :contains(), :has(), :empty. Note: always use with colon.
How could you find all paragraphs that included the text “Hello”?
What if it could be “hello” or “hi” or “hiya”?
- $(“p:contains(‘Hello’));
- $(“p”).filter(function( ) { return pattern.text($(this).text());
}
Select all input elements except those that are hidden:
$(‘:input:not(:hidden)’);
Select both all input elements and all textarea elements in one call:
$(‘textarea, :input’);
Select all images with a common extension of jpg:
$(“img”).filter(function(index){
return /.jpg/.test(this.src);
} // this points to the element not the jquery object
Match all div elements with ids like id=”card-1”, id=”card-2”, id=”card-3” etc
$(“div[ id^=’card-‘ ]”);
What does [attr$=val] do?
It matches elements that have a particular attribute with a name ending in val. E.g. $(“[id$=’card’]”) will match div id=”big-card”, id=”wide-card”, and id=”small-card”
How would you select every element that had a css background image?
$(“*”).filter(function(i){
return !! $(this).css(“backgroundImage”);
}; // !! not necessary but helps clarify
How can you extend jQuery’s selector expressions, say, if you wanted to create a custom filter for finding only inline elements.
Use the jQuery.expr[":"] object: $.expr[":"].inline = function(elem, index, array){ // must return true or false return $(elem).css("display") === "inline"; }
How is the $(selector).each() method different from the javascript array.prototype.forEach() method?
jQuery each() takes a function that takes only one argument, the index. You can stop the loop by returning false at any time. Note that there is a separate utility function called each() which is different. JS forEach() takes a function with 3 params: item, index, array.
How can you convert a jQuery object to a raw DOM object? (e.g. instead of the $(“p”) list, you want the list of DOM nodes).
Use array notation, $(“p”)[ 0 ] will get the first node with nodeName of “paragraph”.
Or use get( ) with optional index (which can be negative). No index will return an array of nodes.
What does $.grep() do?
It’s a filter method for arrays. Like the native filter method but it takes an array and then a function. Ex: newArray = $.grep(oldArray, function(item, index)) { //return true or false }
What is the difference between $.merge() and array.concat()?
merge takes two arrays as arguments and returns the second one concatenated to the first (it changes the first array).
concat() creates a third array that it returns.
How do you get the value of an input field?
$(“someInput”).val();
If $input is an input field, how do you remove whitespace from its contents?
Use the trim utility:
$.trim( $input.val( ));
What is an expando and why can it cause problems?
It’s a property added to an object or DOM node at runtime. It can cause garbage collection problems.
What is the jQuery.data() method?
It allows us to attach data of any type to DOM elements in a way that is safe from circular references and therefore free from memory leaks.