cc young
cc young

Reputation: 20245

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.

Upvotes: 3

Views: 1838

Answers (1)

Quentin
Quentin

Reputation: 944320

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.

Upvotes: 7

Related Questions