yossi
yossi

Reputation: 3164

jQuery :data() selector not working after being set

I expect to match at least one element, but I get zero. What am I doing wrong?

$("tr:first").data("aaa", "333"); // setting the data
console.log($("tr:data(aaa == "333")")); // reading the data

Upvotes: 0

Views: 400

Answers (1)

Jai
Jai

Reputation: 74738

change this:

console.log($("tr:data(aaa, 333)"));

to this only:

console.log($("tr:data(aaa)").data('aaa') === "333");

If you are looking to filter the trs then use .filter() method:

$("tr:data(aaa)").filter(function(i, el){
  return el.data('aaa') === "333"
}).css('color', 'red');

You are actually trying to set the data value again to the tr element in the console.

$('span').data('aaa', 3333);
var value = $('span:data(aaa)').data('aaa')
$('pre').html(value == 3333)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

<span>3333</span>

<pre>pre</pre>

Upvotes: 1

Related Questions