en.javascript.info icon indicating copy to clipboard operation
en.javascript.info copied to clipboard

sync between attributes and properties

Open dagolinuxoid opened this issue 6 years ago • 4 comments

But there are exclusions, for instance input.value synchronizes only from attribute → to property, but not back:

I don't know is it worth or not to mention (as a side note) that the sync between attributes and properties is not going to happen after a property is assigned directly. Example

let inp = document.getElementById('search');
inp.setAttribute('value','initialValue');
inp.value === 'initialValue'; //true
inp.setAttribute('value','changedValue via setAttribute');
inp.value === 'changedValue via setAttribute'; // true;
inp.value = 'cancel all consecutive attempts to sync via setAttribute';
inp.value === 'cancel all consecutive attempts to sync via setAttribute'; // true
inp.setAttribute('value','has no effect on input.value');
inp.value === 'has no effect on input.value'; // false
inp.value === 'cancel all consecutive attempts to sync via setAttribute'; // true

go to the website

dagolinuxoid avatar Nov 05 '19 06:11 dagolinuxoid

You give so many code lines =)

What you'd like to add? There's a note about syncing properties that it happens one way sometimes.

iliakan avatar Dec 01 '19 13:12 iliakan

Probably it's best to keep it as it is. I just wanted to say that:

after changing a property, further manipulation on a corresponding attribute has no effect on the property.

dagolinuxoid avatar Dec 07 '19 19:12 dagolinuxoid

Are you sure? Is that in the spec?

iliakan avatar Dec 07 '19 20:12 iliakan

It's just my observation. I hope it is more clear than my previous example.

let's say we have the html

<input id='field' value='initial'/>

And the js

const field = document.getElementById('field');
console.log(field.value); // initial

// there is no sync here: I only change a value property, value attr is still intact
field.value='further altering a value attr has no effect on the value property'; 

console.log(field.getAttribute('value')); // 'initial'

field.setAttribute('value', "I can't change the value property by setting a value attr")

// further altering a value attr has no effect on the value property
console.log(field.value);

dagolinuxoid avatar Dec 08 '19 12:12 dagolinuxoid