2

I'm looking for a specific element which contain a known data attribute. The actual value of the data could be anything. Example:

<div id="myObject">
   <div>
     <span data-some-data="anything">something</span>
   </div>
<div>

I'm trying to access the span tag in the above example.

I've tried something like:

var $theElementINeed =  $('#myObject').find("[data-some-data='" + ??? + "']"); 

But the value could be anything. So I don't know how to find the span. Any suggestions?

Rodney Hickman
  • 3,043
  • 11
  • 50
  • 80

2 Answers2

3

If you just want to check for the existence of the attribute then use the has attribute selector

var $theElementINeed =  $('#myObject').find("[data-some-data]");
Arun P Johny
  • 376,738
  • 64
  • 519
  • 520
  • how is find() better than just $('#myObject [data-some-data]") ? i don't understand why it's broken up into two selectors... – dandavis Jan 17 '14 at 00:45
0

Per this! discussion, try:$('element').find('[data-some-data]');

Community
  • 1
  • 1