Skip to content

Input value Property vs Attribute Desync

Quick summary

You read getAttribute('value') after the user typed — it returns the initial default, not the current input.

Why this happens

  • Reading getAttribute('value') instead of the .value property

  • Setting setAttribute('value', x) which does not update the current value in some browsers

  • Confusing the default-value attribute with the live value property

Minimal example

✗ Broken code
const input = document.querySelector('input');
input.value = 'typed';
console.log(input.getAttribute('value')); // old default, not 'typed'
✓ Fixed code
const input = document.querySelector('input');
input.value = 'typed';
console.log(input.value); // 'typed' — current content

getAttribute('value') returns the default from the HTML, not the current content. The .value property always reflects what the user sees and typed.

How to diagnose

  • Compare element.getAttribute('value') with element.value after user input

  • Check whether code reads the attribute or the property

  • Use element.value for the current content in all cases

How to fix

  • Always read element.value for the current input content

  • Set element.value = x to update the displayed value

  • Use setAttribute only for the initial default value

How to prevent

  • Always use the .value property for input content, never getAttribute

  • Remember: attribute = default, property = current

Related Resources

Related Glossary

Related Lessons

  • DOM

    Learn about form elements.

← Back to language