What does $ ($ (this) .attr ('href')) mean in jQuery?

I'm new to jQuery and understand the basics, but I'm having problems with certain parts of the page, so I need to fill in some knowledge gaps.

I understand that $(this).attr('href') will get the href attribute inside the current focused / clicked element.

But what if it says: $($(this).attr('href')); ?

I did not write his code, which I have in the script, and I'm not sure if this is just an error or intentional.

I have a feeling that it’s pretty simple, but how to search for $($()) ? And when I search for $($(this).attr('href')) , all I get is the documentation for the original sentence, which I already understand.

Is it just a typo or a separate precedent?

+5
source share
1 answer

As you said, $(this).attr('href') returns the matched href element. This is a string. Then the string is passed to $ , which does not know that href treats it like any other string: it parses it, decides whether it looks like HTML or a selector, then returns the received jQuery instance.

Presumably this is in some context where href is also either a valid selector or valid HTML. As mentioned in the comments, a likely candidate is a line of type #something , which, like href, refers to the scroll position of the element with the identifier something , and indicates jQuery to select the same element as a selector.

+6
source

Source: https://habr.com/ru/post/1276294/


All Articles