Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in javascript, difference input.value=val versus input.setAttribute('value',val)?

In javascript, is what is the difference between setting an HTMLElement property with assignment as versus using setAttribute(). The following is from a chrome session, leading me to believe there is a difference:

> i = document.createElement('input');
<input>
> i.value = 'abc';
"abc"
> i
<input>​
> i.setAttribute('value','abc');
undefined
> i
<input value=​"abc">

What exactly is the difference? Is it the type of thing that bytes you in the ass?


answer right on.

chrome displays attributes, so this led to my confusion.

like image 559
cc young Avatar asked Aug 06 '26 00:08

cc young


1 Answers

In javascript, is what is the difference between setting an HTMLElement property with assignment as versus using setAttribute()

It depends on the property.

The value property reflects the current value, the value attribute reflects the default value.

Some properties map directly onto attributes.

like image 68
Quentin Avatar answered Aug 08 '26 15:08

Quentin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!