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?
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)
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).
$('. common-end div');
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.
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.
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')
.
$($(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"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With