Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to get the attribute value using jquery method

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

like image 805
3gwebtrain Avatar asked May 08 '26 02:05

3gwebtrain


2 Answers

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

        })
like image 162
rajesh kakawat Avatar answered May 09 '26 14:05

rajesh kakawat


It should be $("div.label:eq(0)"); instead of $("div.label")[0];

var x =  $("div.label:eq(0)");

Updated fiddle here.

like image 21
codingrose Avatar answered May 09 '26 14:05

codingrose