0

I want to be able to select a div based on the presence of a span element that has an attribute.

<div>
   <a>blabla</a>
   <p>blabla></p>
   <span data-stackoverflow="hello"></span>
   <img>blabla</img>
</div> 

How do i do that?

How is this question the same as the question this question's supposed to be a duplicate of? That question is about elements that hold certain attributes on which the elements are selected.

I want to be able to select a DIV based on a CHILD SPAN!! that has an attribute!!

edit

$( "div" ).has( 'span[data-stackoverflow="' + somevalue+ '"]' ).show();

This one workes for me however in some occasions it affects other div's as well.

ingridsede
  • 329
  • 1
  • 5
  • 19
  • I agree, your question is slightly different. Anyway, use [.has()](http://api.jquery.com/has/) on a search for divs using the method in the duplicate or use the method in the marked duplicate and call [.parent()](http://api.jquery.com/parent/) or [.parents()](http://api.jquery.com/parents/) on the match(es) – Daniel Jun 13 '14 at 13:47
  • Thanks for that but it's not clear to me how then to select based on the value that the attribute holds. – ingridsede Jun 13 '14 at 13:58

1 Answers1

0

You could try:

$('div').filter(function () {
    return $(this).find('span').data('stackoverflow') == 'hello';
}).show();

jsFiddle example

j08691
  • 204,283
  • 31
  • 260
  • 272