-2

I set data-attribute with following:

jQuery('.question').data('status', status_value); // status_value can be wrong or correct

Now I have two lines going one after another

jQuery('.question').data('status') // returns value wrong or correct, e.g. data-attribute 'status' exists and filled with value
jQuery(".question[status='wrong']") // returns undefined

Why the shortened syntax doesn't work?

My task is to get the element which has data-attribute value as "wrong"

Alex F
  • 183
  • 2
  • 14

1 Answers1

0

jQuery(".question[data-status='wrong']") select all the elements that have the attribute set to wrong but in the code jQuery('.question').data('status', status_value); you are setting the data object of the dom node not the attribute, to set the attribute use attr()

jQuery('.question').attr('data-status', status_value);

madalinivascu
  • 32,064
  • 4
  • 39
  • 55