Which is more efficient: .parent().parent().parent() ~or~ parents(“.foo”) ~or~ closest(“.foo”)

Here’s an analyzation: parent() walks just one level up in the DOM tree. parents(“.foo”) walks up to the root and selects only those elements that match the given selector .foo. closest(“.foo”) walks up to the root but stops once an element matches the selector .foo. So I would choose the last one, closest(“.foo”). The reason: … Read more

jQuery difference between :eq() and :nth-child()

:eq() Select the element at index n within the matched set. The index-related selectors (:eq(), :lt(), :gt(), :even, :odd) filter the set of elements that have matched the expressions that precede them. They narrow the set down based on the order of the elements within this matched set. For example, if elements are first selected … Read more

(jquery) Blackout the entire screen and highlight a section of the page?

See example of the following here → No need for a plugin. This can be accomplished with very little jQuery code, showing a blackout overlay with the selected div at a z-index above it: $(‘.expose’).click(function(e){ $(this).css(‘z-index’,’99999′); $(‘#overlay’).fadeIn(300); }); $(‘#overlay’).click(function(e){ $(‘#overlay’).fadeOut(300, function(){ $(‘.expose’).css(‘z-index’,’1′); }); }); According to the following HTML & CSS… just add the expose … Read more

jQuery – equivalent to each(), but for a single element

You can always reference the jQuery object in a variable: var $el = $(‘#element’); …then manipulate it. $el.doSomething(); // call some jQuery methods from the cached object $el.doSomethingElse(); If the reason you wanted .each() was to reference the DOM element as this, you don’t really need the this keyword to do it, you can simply … Read more