Daniel Pietzsch

Personal blog. Mostly photos.

$input.setAttribute vs. $input.value

If you want to set the current value of an HTML input element using JavaScript, use .value instead of .setAttribute to do so. Example:

//sets the current value
document.getElementById('FIELD-ID').value = 200
//sets the value attribute, i.e. the default value
document.getElementById('FIELD-ID').setAttribute('value', 200)

This makes sense now, but was something I was a little confused about recently. Of course, setAttribute sets the default value. Because that’s what the value attribute on an input is for. I meant to change the current value, though, but was trying to use setAttribute for that (which strangely enough worked for my purposes in Safari and Firefox, but not Chrome).

Anyhow, that’s my little reminder to self and my little piece of writing for the day.