Steve Kim
Steve Kim

Reputation: 5601

jQuery find div with data attr

So, I have following markup:

<div class="yay" data-pi="23"></div>
<div class="yay" data-pi="24"></div>
<div class="yay" data-pi="25"></div>
<div class="yay" data-pi="26"></div>

I am looking for a specific div with data-pi"26".

This is what I have:

var my_class = jQuery('.yay').find("[data-pi="26"]");

In other words, I want to find the div that contains data-pi=26. Do I have it right?

thanks

Upvotes: 2

Views: 143

Answers (2)

pherris
pherris

Reputation: 17743

FYI, you don't need jQuery for this:

var my_div = document.querySelector("div[data-pi='26']")

http://youmightnotneedjquery.com/

MDN reference: https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Getting_Started/Selectors

Upvotes: 2

Kevin Kopf
Kevin Kopf

Reputation: 14230

You should use the quotation marks right. This is what I would have used:

var my_class = $("div.yay[data-pi='26']");

.find() actually finds the elements inside .yay element, so try my code.

Upvotes: 1

Related Questions