-1

This gets the attribute

console.log(doc.find('[data-monitor="total_gross"]').data('amount'));

This should set the attribute, but doesn't

doc.find('[data-monitor="total_gross"]').data('amount', total_gross);

What am I doing wrong?

clarkk
  • 27,151
  • 72
  • 200
  • 340
  • use `.attr()` instead – guradio Jun 23 '16 at 08:17
  • What is `doc`? And keep in mind, setting a `data` will not be shown in you html, its jQuery internal. Otherwise use `attr('data-amount', total_gross)` instead of `data`. – eisbehr Jun 23 '16 at 08:18
  • Your code seems to work fine: https://jsfiddle.net/RoryMcCrossan/kj95rfkh/. Please add some more details of the exact issue. Note that `data()` does not change anything in the DOM, it just sets the value in jQuery's internal data object cache. So long as you use `data()` as a getter/setter then there is no issue. – Rory McCrossan Jun 23 '16 at 08:18

1 Answers1

1

To update in the markup you need to use attr() method

doc.find('[data-monitor="total_gross"]').attr('data-amount', total_gross);

Refer : jQuery Data vs Attr?

Community
  • 1
  • 1
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
  • 1
    il mark sa dup on the link you gave ok? – guradio Jun 23 '16 at 08:20
  • He does not say, that he want to have it in the markup. His original post is not even wrong. So saying `you need to use` is not correct. He is not *need* to. He can ... – eisbehr Jun 23 '16 at 08:21
  • Important to point our that `.data()` -will- update the data attribute, and will be accessible later, just not the actual visible attribute on the DOM element – casraf Jun 23 '16 at 08:23
  • @eisbehr : for updating in markup he __need__ to... – Pranav C Balan Jun 23 '16 at 08:25
  • @PranavCBalan Yes. But who was asking for updating the markup? – eisbehr Jun 23 '16 at 08:26
  • @eisbehr : if he is saying `.data('amount', total_gross);` is not working.... from that we can predict that... let's wait for the response from asker :) – Pranav C Balan Jun 23 '16 at 08:29