-5

How to target an html attribute with JS by his id and make it equal to some number.

ISAAC
  • 129
  • 7

1 Answers1

1

document.getElementById('foo').setAttribute('bar', 0);

Where foo is the ID of your element and bar is the name of the attribute you want to change.

Chris Riebschlager
  • 1,293
  • 1
  • 8
  • 12
  • I think i didn't explain myself right. I want to target an ID and set his value to some number. something like: document.getElementById('foo') = 0; but in a proper way. – ISAAC Dec 25 '17 at 20:26
  • So you want to change an element's ID to the number 0? That's a bad idea: https://stackoverflow.com/questions/70579/what-are-valid-values-for-the-id-attribute-in-html However, you could change its id much the same way I posted above to a safe name including the number zero. `document.getElementById('foo').setAttribute('id', 'element-0');` – Chris Riebschlager Dec 25 '17 at 20:28