javascript - setAttribute() doesn't work the way I expect it to -


this line of code:

document.getelementbyid('01').getelementsbytagname("input")[0].setattribute("value", "1"); 

works fine when "input" doesn't have value. when input has value, wouldn't change value. why that?

sounds faced confusing difference between element property value , value attribute. not same things.

the thing value-attribute serves purpose of default value, if element has property value changing value-attribute not reflected in ui.

documentation says this:

using setattribute() modify attributes, notably value in xul, works inconsistently, attribute specifies default value. access or modify current values, should use properties. example, use elt.value instead of elt.setattribute('value', val).

so demonstrate situation consider little demo:

document.getelementbyid("01").getelementsbytagname("input")[0].value = 'property set';    document.getelementbyid("01").getelementsbytagname("input")[0].setattribute("value", "two");
<div id="01">      <input type="text" />  </div>

in above snippet value attribute indeed updated value two , can verify if try read getattribute('value'), value-property takes precedence on attribute, later not rendered.


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -