i want to use the html data-*
attributes and have some images like this:
<img src="http://placehold.it/100.png" data-selected="true">
<img src="http://placehold.it/100.png" data-selected="false">
<img src="http://placehold.it/100.png" data-selected="false">
<img src="http://placehold.it/100.png" data-selected="true">
<img src="http://placehold.it/100.png" data-selected="false">
how can i now just only get the ones with data-selected="true"
?
I tried:
$("img").each(function(){
if($(this)).attr("data-selected") == "true") {
//do something
}
}
but this seems not to be the best way to me. Is there a direct selector where i can do something like
$("img data-selected=true") ?
thanks for your help!!
$("img[data-selected='true']")
but quoting of value isn't obligatory.
PS: it is called CSS attribute selector
.
Well for one thing you should use .data(...)
$("img").each(function(){
if($(this)).data("selected") == "true") {
//do something
}
}
Or you can use:
$("img[data-selected='true']").something...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With