Reputation: 1399
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
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:
input
event triggers with every change, not just when the value is "entered". textarea
and select
elements.Upvotes: 1
Reputation: 207501
If you want the attribute to update, you got to set the attribute.
<input onchange="this.setAttribute('value', value)" />
Upvotes: 2