javascript - Is it possible to change dom-element property types? (Number, String etc.) -
consider input field
<input type="number" id="output" disabled="true">
is possible ?
var output = document.getelementbyid("output"); output.value = number(0);
i tested , has no effect (+= operator still concating strings). there way change variable type of dom-element property?
what happens in line
document.getelementbyid("output").value = number(0);
and why have no effect on type?
does apply dom-objects or there other cases invariability applies (or can actively define variable)?
ps: know can use temporary variable avoid problems in general, want know why dom property unaffected output.value = number(0);
instead of assigning value directly dom element attribute should use setattribute function of element.
var output = document.getelementbyid("output"); output.setattribute('value',number(0));
why have no effect on type?
the dom element's attributes part of html , because of html structure attribute values should quoted (https://www.w3.org/tr/xhtml1/diffs.html#h-4.4), therefore value set property stored string.
this means if you're explicitly giving value number(0) atribute 'value' @ end stored in html structure this:
<input type="number" id="output" disabled="true" value="0"/>
and can see quoted, string representation of attribute value, 0 string representation "0", if try set attribute type e.g object, have this.
//setting custom attribute value: {a:10} document.getelementbyid('output').setattribute('myattr',{a:10})
html:
<input type="number" id="output" disabled="true" myattr="[object object]">
Comments
Post a Comment