0

I'm looking in parent divs for child elements that have a specific HTML5 data attribute. This code was working fine:

$('#parent').find('*[data-something]').css('color', 'red');

Then I found out that you can find HTML data attributes using Jquery's data object http://api.jquery.com/data/#data-html5

So I tried...

$('#parent').find($("#parent").data("something")).css('color', 'red');

...and it doesn't work. Does anyone know why?

JSFIDDLE: http://jsfiddle.net/Qct9v/

Note: I have to use find() because I need to search child elements.

1
  • You have misunderstood what .data does. Commented Jan 12, 2014 at 2:43

1 Answer 1

2

It doesn't work because data() is not a filter method, it is a getter/setter method used to get/set the data value.

If you want to use jQuery data then try something like

$("body").find('*').filter(function(){
    return $(this).data('something') != undefined
})
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! In that case I guess it will be easier to pass the data attribute like I did in the first code. Less code.
@CyberJunkie yes it is and faster

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.