ChrisA
ChrisA

Reputation: 293

Attribute value VS property value

I have an <input> element which is loaded with a default value. Later on, i change that value via jQuery's input.val("different value").

When I console.log() the element, I see this in firebug:

Object[input.cs_required.form-control.input-sm property value = "12/29/2014" attribute value = "12/02/2014"]

Upvotes: 0

Views: 2789

Answers (2)

Felix Kling
Felix Kling

Reputation: 817218

As you said, you are providing a default value via the value HTML attribute. That will be the initial value of the DOM element's value property. Changing the propery does not affect the attribute.

The value of HTML attributes is often used as initial value for the corresponding DOM property.

Upvotes: 1

Mechkov
Mechkov

Reputation: 4324

Quick example of the difference:

jQuery('#something').val() changes the *.value property. jQuery('#something').attr('value','neValue') changes the attribute on that component.

There is an important difference between the two. Take a look at this for more info.

What's the difference between jQuery .val() and .attr('value')?

The only thing you have to worry about is whether you are using/assigning/utilizing the correct item - property or attribute.

Upvotes: 1

Related Questions