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
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.

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.