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?
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") );
});
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