Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between $('#tabs a') and $('#tabs').find('a')

I have following structure

<ul id="tabs" class="nav nav-tabs">     <li><a href="#aaa" hashval="aaa">AAA</a></li>     <li><a href="#bbb" hashval="bbb">BBB</a></li>     <li><a href="#ccc" hashval="ccc">CCC</a></li>     <li><a href="#ddd" hashval="ddd">DDD</a></li> </ul> 

Now I am operating on the anchor tag by following code and which is working fine.

$('#tabs a[href="#ddd"]').tab('show'); 

I am using pycharm which adds warning for the line by saying "Preface with ID selector". When I click it, pycharm changes to following

$('#tabs').find('a[href="#ddd"]').tab('show'); 

Both are working fine but I don't understand the difference.

What is the difference in both or more specifically what is difference between $('#tabs a[href="#ddd"]') and $('#tabs').find('a[href="#ddd"]')?

like image 334
Ansuman Bebarta Avatar asked Jul 10 '13 11:07

Ansuman Bebarta


People also ask

What is the difference between this and $( this?

Difference between this and $(this)The this Keyword is a reference to DOM elements of invocation. We can call all DOM methods on it. $() is a jQuery constructor and in $(this), we are just passing this as a parameter so that we can use the jQuery function and methods.

What does $this mean in jQuery?

this is a reference to the html DOM element that is the source of the event. $(this) is a jQuery wrapper around that element that enables usage of jQuery methods. jQuery calls the callback using apply() to bind this .

What are $( and $(( )) in bash?

$(...) is an expression that starts a new subshell, whose expansion is the standard output produced by the commands it runs. This is similar to another command/expression pair in bash : ((...)) is an arithmetic statement, while $((...)) is an arithmetic expression. Follow this answer to receive notifications.

What is the difference of and in HTML?

There is no difference at all. Do you have an example of what you're trying to do? They should be interchangeable. If you've something different please post it.


2 Answers

$("#tabs a") evaluates from right to left - which is the native direction of both Sizzle selector engine and querySelectorAll - i.e. first it finds all of the anchor elements in the page and then narrows it down to those under #tabs.

$("#tabs").find("a") evaluates - more intuitively - from left to right, i.e. first it finds #tabs, and then only the anchor elements under it.

Clearly the latter would yield better performance, but it would only be noticeable accumulatively; that is, if you run thousands of queries. Otherwise, the difference is negligible.

like image 106
Matanya Avatar answered Sep 23 '22 01:09

Matanya


As stated in "Increase Specificity from Left to Right":

A little knowledge of jQuery’s selector engine is useful. It works from the last selector first so, in older browsers, a query such as:

$("p#intro em"); 

loads every em element into an array. It then works up the parents of each node and rejects those where p#intro cannot be found. The query will be particularly inefficient if you have hundreds of em tags on the page.

Depending on your document, the query can be optimized by retrieving the best-qualified selector first. It can then be used as a starting point for child selectors, e.g.

$("em", $("p#intro")); // or $("p#intro").find("em"); 

But Test case says $("#tabs > a") would be fastest

like image 37
Rohan Kumar Avatar answered Sep 22 '22 01:09

Rohan Kumar