PDA

View Full Version : does document.getElementById work with form elements



zedan85
Oct 20th, 2011, 07:23 PM
I wanted to know if document.getElementById works while accessing form elements.
I tried doing this just for testing purposes

This code doesnt work
function validateForm()
{
var val = document.getElementById("id_login").getAttribute("value");
alert(val);
return false;
}

but this does
function validateForm()
{
alert(document.myForm.text_login.value );
return false;
}

Why doesnt document.getElementByid work with form objects.it works with all non form HTML objects..

Philip M
Oct 20th, 2011, 07:36 PM
It works just fine with form elements if you use the correct syntax:


function validateForm() {
var val = document.getElementById("id_login").value; // assuming that id_login is a textbox
alert (val);
return false;
}


You should prefer to address form elements by name as only those with a name attribute are submitted to the server.


function validateForm() {
var val = document.myForm.id_login.value;
alert (val);
return false;
}


All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.