Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting href value via jQuery

I want to get the value of href and hash of an a tag. When I do

$(document.body).on('click',"a",function(event){ 
    console.log($(this));
}); 

I see an object with something like

[a.internalLink, context: a.internalLink, jquery: "1.10.2", constructor: function, init: function, selector: ""…]
    0: a.internalLink
        accessKey: ""
        attributes: NamedNodeMap
        ...
        hash: "#abc.1.2"
        ...
        href: "http://www.example.com/page.html#abc.1.2
        ...

But when I tried to get the value by console.log($(this).href), it just doesn't work (prints out "undefined"). How can I get it?

like image 587
Mika H. Avatar asked Jan 27 '26 13:01

Mika H.


1 Answers

If you want to refer to specific attributes of an element, you can use the attr function of jQuery:

$(document.body).on('click',"a",function(event){ 
    console.log( $(this).attr("href") );
});
like image 178
jotaen Avatar answered Jan 29 '26 03:01

jotaen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!