0

I'm trying to update my data attribute, but my code doesn't work. Don't see what i'm missing

<tr class="sell-figures" rel="sell-id">
      <th></th>
      <td class="" data-sell-id="1"></td>
      <td class="" data-sell-id="12"></td>
      <td class="" data-sell-id="123"></td>
      <td class="" data-sell-id="1234"></td>
</tr>

and here is my jquery

$(".sell-figures tr[rel=sell-id] td:nth(3)").data("sell-id", '999');

I'm trying to change 1234 to 999. what am i doing wrong here? Thanks

user2636556
  • 1,905
  • 4
  • 32
  • 61
  • Are you trying to update the value of the data-sell-id attribute? jquery .data associates information with the element, but doesn't update the attribute value. – Joel Jul 29 '14 at 16:58
  • http://stackoverflow.com/questions/22753629/jquery-get-html-5-data-attributes-with-hyphens-and-case-sensitivity – Kiran Jul 29 '14 at 16:59

2 Answers2

0

This should do it. It will select item with class sell-figures and then on the 4th child will set attribute to data-sell-id to 999

$(".sell-figures>td:nth(3)").attr("data-sell-id", '999');
Matas Vaitkevicius
  • 58,075
  • 31
  • 238
  • 265
  • That won't work, it will look for an attribute named `data-data-sell-id`. – Barmar Jul 29 '14 at 16:59
  • 1
    Really? Is that a new CSS or jQuery selector? I'm not familiar with it. – j08691 Jul 29 '14 at 17:02
  • 1
    @user2636556 You can use `.data()` to read the attribute, but then jQuery caches it internally and doesn't update it in the DOM. – Barmar Jul 29 '14 at 17:22
0

Your selector is incorrect. Use:

$("tr[rel=sell-id].sell-figures td:nth-child(3)").data("sell-id", '999');
j08691
  • 204,283
  • 31
  • 260
  • 272