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,133
  • 11
  • 53
  • 83
  • possible duplicate of [How do I target elements with an attribute that has any value in CSS?](http://stackoverflow.com/questions/9271424/how-do-i-target-elements-with-an-attribute-that-has-any-value-in-css) – Ciro Santilli OurBigBook.com Feb 04 '14 at 21:51

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
  • 384,651
  • 66
  • 527
  • 531
  • 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