Loop over array of elements, change data attributes

791 views Asked by At

I am trying to loop through an array of elements with a particular data tag value so that I can change the value of the data tag inside of the loop. I thought it would be fairly simple but I'm struggling. There are elements with the data tag row that have the value of "new". All I'd like to do is change the values from "new" to "old". Here is the code that I have. The jQuery selection of data-row='new' returns 3 elements. But when I check the values of these data tags, they have not changed, and there are no elements with data tags of "[data-row='old']". What am I missing?

var setNewRowToFalse = function(e) {
  $.each($("[data-row='new']"), function(idx, elem) {
    $(elem).data("row", "old");
  });
};
2

There are 2 answers

0
zaynetro On BEST ANSWER

Use .attr('data-row', 'old') instead.

Check data function docs.

1
im_benton On

I don't even think you need the loop...

$("[data-row='new']").attr('data-row', 'old')