0

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.

2 Answers 2

3
$(".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>

Sign up to request clarification or add additional context in comments.

3 Comments

I'm getting f.fn.init [button.drawer-face] as the output from $(".drawer-face").eq(6). If I go into that element I can see the fields but when I try to access them with attr I get undefined.
Please create a working example of this issue in your question. @Blue
@Blue I've added an example to the answer to show that this approach does work.
1

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

1 Comment

I'm getting f.fn.init [button.drawer-face] as the output from $($(".drawer-face")[6]). If I go into that element I can see the fields but when I try to access them with attr I get undefined.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.