javascript – How to get href value using jQuery?

javascript – How to get href value using jQuery?

You need

var href = $(this).attr(href);

Inside a jQuery click handler, the this object refers to the element clicked, whereas in your case youre always getting the href for the first <a> on the page. This, incidentally, is why your example works but your real code doesnt

You can get current href value by this code:

$(this).attr(href);

To get href value by ID

$(#mylink).attr(href);

javascript – How to get href value using jQuery?

Its worth mentioning that

$(a).attr(href); // gets the actual value
$(a).prop(href); // gets the full URL always

Leave a Reply

Your email address will not be published.