I've read this, so this is not a duplicate. All the solutions proposed don't work jQuery how to find an element based on a data-attribute value?
Here's what I'm doing on the console of Chrome:
$('table#ct_ennemies_2 td').each(function() { var t=$(this).data('to-shoot'); console.log(t == "1") }); Then I get a result: one cell is marked with data('to-shoot') = 1. Great. Now if I try to find by data attribute like this:
$('table#ct_ennemies_2 td[to-shoot="1"]').each(function() { console.log($(this)) }); I get an empty result:
[] Same if I try
$('table#ct_ennemies_2 td[to-shoot=1]').each(function() { console.log($(this)) }); I get an empty result:
[] Here's what you can do on the console log of Chrome:
>> $('table#ct_ennemies_2 td').first().data('to-shoot','1'); [<td ...blablah >@</td>] >> $('table#ct_ennemies_2 td').first().data(); Object {toShoot: "1"} >> $('table#ct_ennemies_2 td').first().data('to-shoot'); "1" >> $('table#ct_ennemies_2 td[to-shoot="1"]'); [] >> $('table#ct_ennemies_2 td[to-shoot]'); [] >> $('table#ct_ennemies_2 td[data-to-shoot]').each(function() { console.log($(this)) }); [] >> $('table#ct_ennemies_2 td[data-to-shoot=1]').each(function() { console.log($(this)) }); [] >> $('table#ct_ennemies_2 td[data-to-shoot="1"]').each(function() { console.log($(this)) }); [] >> $('table#ct_ennemies_2 td[data-toShoot="1"]').each(function() { console.log($(this)) }); [] >> $('table#ct_ennemies_2 td[toShoot="1"]').each(function() { console.log($(this)) }); [] >> $('table#ct_ennemies_2 td[toShoot=1]').each(function() { console.log($(this)) }); [] >> td = $('#ct_ennemies_2 td').filter(function() { >> return $(this).data('to-shoot') === 1; >> }); [] >> td [] My question is: how to apply properly a filter that returns the expected td which contains the data to-shoot=1?
var t = $(this).attr('data-to-shoot');does it return the same thing.$('table#ct_ennemies_2 td[data-to-shoot=1]')as Kyle suggested should be working.