I am trying to get a attribute value from my dom element, but i am getting result as:
Uncaught TypeError: Object #<HTMLDivElement> has no method 'attr'
I am not able to get the attribute value at all.. what is the issue..?
here my my code :
HTML:
<div class="label" aria-label="Logo">
<div class="trunc-string">
<div class="start denali-tooltip tooltip-on-truncate"><span>Logo</span></div>
</div>
</div>
jQuery:
var x = $("div.label")[0];
console.log (x.attr("aria-label"));
any one direct me in the right way please..?
Live Demo
try something like this,
$(function(){
// will give you pure javascript(DOM) object,so use getAttribute().
var x = $("div.label")[0];
console.log(x.getAttribute("aria-label"));
//using jquery
console.log($(x).attr("aria-label"));
})
It should be $("div.label:eq(0)"); instead of $("div.label")[0];
var x = $("div.label:eq(0)");
Updated fiddle here.
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