Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the extra "$()" in "$($())" selector do?

I want to implement a jQuery scrollspy to one of the projects I am working on.

I found this jsfiddle (https://jsfiddle.net/mekwall/up4nu/) which I managed to implement into my project. I wish to modify it but am stuck at trying to understand what this bit of code means.

var topMenu = $("#top-menu"),
menuItems = topMenu.find("a"),
   // Anchors corresponding to menu items
   scrollItems = menuItems.map(function(){
     var item = $($(this).attr("href"));
     if (item.length) { return item; }
});

I know in general, the code will search for all "a" links, find the href via the attr() function, and if it exists, add it into the map. What I don't understand is

 $($(this).attr("href"));

What does the extra $() selector mean? I understand that

$(this).attr("href");

means selecting/retrieving the href for this element. What does the $() do? Is it a nested selector? I tried googling but I couldn't find answers to it, or my google-fu is not up to par.

Also, does

$($(this).attr("href"));

only retrieve links in the following format index.html#section-one or #section-one?

Update

Also, how would '.length' determine if the element exists in the DOM? When I checked the console logs, a href link with 'index.html#section-one' would return a length of 0, while one with only '#section-one' would return with a length of 1. Why does that happen?

like image 368
Jacob Tan Avatar asked Aug 18 '15 06:08

Jacob Tan


People also ask

What is $() in jQuery?

The jQuery syntax is tailor-made for selecting HTML elements and performing some action on the element(s). Basic syntax is: $(selector).action() A $ sign to define/access jQuery. A (selector) to "query (or find)" HTML elements. A jQuery action() to be performed on the element(s)

What does jQuery selector return?

The jQuery Object: The Wrapped Set: Selectors return a jQuery object known as the "wrapped set," which is an array-like structure that contains all the selected DOM elements. You can iterate over the wrapped set like an array or access individual elements via the indexer ($(sel)[0] for example).

Which code is used to search and select items that are multiple levels deep within a hierarchy?

$('. common-end div');

What is jQuery length?

The length property in jQuery is used to count the number of elements in the jQuery object. The size() function also returns the number of elements in the jQuery object.


3 Answers

That code assumes that the href attribute is going to be an id reference, like #about. The outer $() goes ahead and grabs that #about element for you.

like image 94
Karl Wilbur Avatar answered Oct 07 '22 14:10

Karl Wilbur


In your case, the href are anchor names (or fragment identifiers), which conveniently double as id selectors. By referencing the href attribute and then wrapping that with $(), you're effectively selecting an element with that id.

So, <a href='#faq'> would normally jump to an named anchor "faq", but by wrapping the attr value of href, you're selecting $('#faq').

like image 26
Jack Avatar answered Oct 07 '22 13:10

Jack


$($(this).attr("href"));

In above expression $(this).attr("href") will give href attribute of the specified html element which is string. If you want to convert this string to jQuery object and do further manipulation, you can achieve it using additional $() like

$($(this).attr("href"))
like image 25
prashant Avatar answered Oct 07 '22 12:10

prashant