Systems Rebooter
Systems Rebooter

Reputation: 1399

Set value attribute in HTML

I am trying to set value attribute in HTML when it gets changed. I need this because I export HTML code for importing it later. I tried the following code:

<input onchange="this.value = value" />

And would like to have the following code so value gets auto-filled after import:

<input onchange="this.value = value" value="some-value" />

There is lots of lines like above but whatever I tried value just doesn't get set.

Upvotes: 1

Views: 91

Answers (2)

trincot
trincot

Reputation: 350147

Alternatively, you can add this JavaScript code and omit the onchange HTML attribute:

document.addEventListener("input", e => e.target.setAttribute('value', e.target.value)); 
<input>

Notes:

  • the input event triggers with every change, not just when the value is "entered".
  • this works for all input elements on the page, including textarea and select elements.

Upvotes: 1

epascarello
epascarello

Reputation: 207501

If you want the attribute to update, you got to set the attribute.

<input onchange="this.setAttribute('value', value)" />

Upvotes: 2

Related Questions