Blue
Blue

Reputation: 1448

How to get attributes of individual elements in jquery array

I am getting an array of element in jquery with this $(".drawer-face")

If I use $(.drawer-face")[x] I get the x element with .drawer-face returned to me.

I want to be able to check attributes within the x element but I get

Uncaught TypeError: $(...)[6].attr is not a function at <anonymous>:1:22 when I use $(".drawer-face").[6].attr("title") for example.

Upvotes: 0

Views: 52

Answers (2)

Taplar
Taplar

Reputation: 24965

$(".drawer-face").eq(x).attr(...)

Use the eq(#) method to keep the element as a jQuery object.

var $drawers = $('.drawer-face');

console.log( $drawers.eq(2).attr('title') );
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<a href="#" class='drawer-face' title="title 1">Title 1</a>
<a href="#" class='drawer-face' title="title 2">Title 2</a>
<a href="#" class='drawer-face' title="title 3">Title 3</a>
<a href="#" class='drawer-face' title="title 4">Title 4</a>
<a href="#" class='drawer-face' title="title 5">Title 5</a>

Upvotes: 3

Rajan
Rajan

Reputation: 1497

You can do like this:

$($(".drawer-face")[6]).attr("title")

$(".drawer-face")[6] gives html dom and jQuery .attr() won't work on that


You can use JS and jQuery together like

$(".drawer-face")[6].getAttribute("title");

Upvotes: 1

Related Questions