0

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?

Community
  • 1
  • 1
Olivier Pons
  • 15,363
  • 26
  • 117
  • 213
  • 1
    If you do `var t = $(this).attr('data-to-shoot');` does it return the same thing. – adeneo Apr 18 '14 at 19:56
  • I don't understand why the linked stackoverflow question doesn't help you, it is the normal way to do it. Maybe if you made a jsfiddle we could see why nothing is returned with your selector, but `$('table#ct_ennemies_2 td[data-to-shoot=1]')` as Kyle suggested should be working. – alxscms Apr 18 '14 at 20:00

2 Answers2

2

data attributes begin with data-

$('table#ct_ennemies_2 td[data-to-shoot=1]')

Note: this only works if you manually added the data attribute in the markup or via attr('data-to-shoot', 1). If it was applied via data('to-shoot', 1) you will need to use Bills' answer.

example fiddle

Fiddle contents:

<div class="test"></div>

$(function(){
  var d = $('div.test');

  d.data('to-shoot', 1);

  alert($('div[data-to-shoot=1]').length); // 0

  d.attr('data-to-shoot', 1);

  alert($('div[data-to-shoot=1]').length); // 1

  var divs = $('div').filter(function(){
    return $(this).data('to-shoot') == 1; 
  });

  alert(divs.length); // 1
});
Community
  • 1
  • 1
Kyle
  • 21,978
  • 2
  • 60
  • 61
  • It doesn't work (I've updated my question with samples tried) – Olivier Pons Apr 18 '14 at 20:04
  • @OlivierPons see my edit as well as [Bill's](http://stackoverflow.com/a/23161306/640577) answer for a solution. – Kyle Apr 18 '14 at 20:05
  • You're right, your solution works with `$('table#ct_ennemies_2 td').first().attr('data-to-shoot','1');` then get it with `$('table#ct_ennemies_2 td[data-to-shoot="1"]')` – Olivier Pons Apr 18 '14 at 20:14
1

I would use filter since .data does not apply the data to an actual attribute, but an inner hash table.

var $td = $('#ct_ennemies_2 td').filter(function() {
  return $(this).data('to-shoot') === 1;
});

Also, pet peeve, the table before your id selector isn't needed.

Bill Criswell
  • 32,161
  • 7
  • 75
  • 66