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"; }