Jump to content

getAttribute("value") versus .value method


roughie

Recommended Posts

I create markup on the fly via DOM scripting.

Included is an input field of id = "mytimer".

 

I fail to retrieve this field's value with the following code;

It comes up with a zero value:

var timer = document.getElementById("mytimer");

var tval = timer.getAttribute("value");

alert("Value of timer is now set to "+ tval);

 

Yet, were I to use:

alert("Value of timer is now set to "+ document.getElementById("mytimer").value);

I succeed.

 

Why this difference?

Link to comment
https://forums.phpfreaks.com/topic/93127-getattributevalue-versus-value-method/
Share on other sites

That is a trick question.

 

 

Excerpt from: http://www.sitepoint.com/article/client-side-form-validation

 

As the observant reader might have noted, value in elements.item.value is represented as an object. But could it have been reached by elements.item(i).getAttribute('value')? No, that just shows the default value (if any) in a tag, not the value entered by the user.

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.