JavaScript Unable to get the attribute value using jquery method unable to return dimension attribute value

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

Answer:1

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

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

Updated fiddle here.

Answer:2
var x =  $("div.label:eq(0)");
console.log (x.attr("aria-label"));
Answer:3

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

        })
Answer:4
var x =  $("div.label")[0];
//^^^^^ by this you get a object with html of .label


console.log ($(x).attr("aria-label"));
//^^^^^^^^^^^^^^then use as a jquery object and apply their method

see demo

Answer:5

if you multiple div.label element and you want to get value according to its node position, You can go with

var x =  $("div.label:eq(0)");
console.log (x.attr("aria-label"));

Otherwise if there is only one div.labelelement, You can go with

var x =  $("div.label");
console.log (x.attr("aria-label"));
Answer:6

By default, .attr will return the attribute value of the first selected element, if used as getter:

Get the value of an attribute for the first element in the set of matched elements or set one or more attributes for every matched element.

So all you really have to do is

$("div.label").attr("aria-label");

If you want to filter selection to only include the first element (because you want to use it somewhere else), you can use .first:

 var x = $("div.label").first();

or .eq(0):

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

The reason why you get the error is that $(...)[i] returns the selected DOM element at that position. DOM elements don't have an .attr method.

Answer:7

I have the following; var room = function(){ this.entities = new Array(); } var myRoom = new room(); I also have a bunch of entites like this; var entity = function(){ this.title = "A ...

I have the following; var room = function(){ this.entities = new Array(); } var myRoom = new room(); I also have a bunch of entites like this; var entity = function(){ this.title = "A ...

  1. filter array objects javascript
  2. filter array objects angular 6
  3. filter array objects
  4. filter array objects js
  5. filter array objects swift
  6. filter array objects jquery
  7. filter array objects angularjs
  8. filter array objects lodash
  9. filter array objects angular2
  10. array filter objects php
  11. filter array of objects by property
  12. filter array of objects typescript
  13. filter array of objects javascript es6
  14. filter array of objects by property javascript
  15. filter array of objects javascript by key
  16. filter array of objects by key
  17. filter array of objects by key value
  18. filter array of objects javascript by value
  19. filter array of objects based on property
  20. filter array of objects javascript based on property

Apologies if this sounds totally stupid but I'm a total dunce when it comes to coding! I'm trying to make a higher or lower guessing game using a deck of cards (i.e. The player guesses if the next ...

Apologies if this sounds totally stupid but I'm a total dunce when it comes to coding! I'm trying to make a higher or lower guessing game using a deck of cards (i.e. The player guesses if the next ...

I want to attached function on click event on document. My function is attached it but it is calling it also. I don't understand why. Demo $(function () { $('a').click(function () { $(...

I want to attached function on click event on document. My function is attached it but it is calling it also. I don't understand why. Demo $(function () { $('a').click(function () { $(...

I am brand new to Angular, and have read through all the tutorials, but am at the steep point of the learning curve where I'm building a real application for the first time. I want to show a div ...

I am brand new to Angular, and have read through all the tutorials, but am at the steep point of the learning curve where I'm building a real application for the first time. I want to show a div ...