View Full Version : Passing form textbox values?
Iyeck
04-13-2007, 09:33 PM
I wish to get the value of a textbox in a form however, I'm unable to reference the textbox using the name property (I wish to get both the name and value property values). Is there another way to get these properties values? Or pass them as parameters through an onClick event as I'm trying to retrieve these within another javascript function.
johnz
04-13-2007, 10:35 PM
Can we see the code?
coothead
04-13-2007, 11:42 PM
Hi there Iyeck,
and a warm welcome to these forums. :agree:
Here is a basic example...
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
window.onload=function() {
getInfo();
}
function getInfo(){
alert(name= '+document.forms[0][0].name+' value= '+document.forms[0][0].value);
alert(name= '+document.forms[0][1].name+' value= '+document.forms[0][1].value);
alert(name= '+document.forms[0][2].name+' value= '+document.forms[0][2].value);
}
</script>
</head>
<body>
<form action="#">
<div>
<input type="text" name="foo" value="good"/>
<input type="text" name="blah" value="bad"/>
<input type="text" name="dodah" value="ugly"/>
</div>
</form>
</body>
</html>
Iyeck
04-14-2007, 12:56 AM
Hi there Iyeck,
and a warm welcome to these forums. :agree:
Here is a basic example...
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
window.onload=function() {
getInfo();
}
function getInfo(){
alert(name= '+document.forms[0][0].name+' value= '+document.forms[0][0].value);
alert(name= '+document.forms[0][1].name+' value= '+document.forms[0][1].value);
alert(name= '+document.forms[0][2].name+' value= '+document.forms[0][2].value);
}
</script>
</head>
<body>
<form action="#">
<div>
<input type="text" name="foo" value="good"/>
<input type="text" name="blah" value="bad"/>
<input type="text" name="dodah" value="ugly"/>
</div>
</form>
</body>
</html>
Thanks for the welcome :)
It worked great except for one thing I should clarify further...
The amount of textboxes always change because my php page is a shopping cart thus, a user can update textbox with a id of 10 (which are in name and value) and nothing would be changed as a result. Is there anyway to switch around the values on the "fly"? That's why I mentioned parameters because the user can do multiple things.
Thanks
coothead
04-14-2007, 06:11 AM
Hi there Iyeck,
Is there anyway to switch around the values on the "fly"?
Why, certainly. :agree:
Take a look at this basic example...
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
window.onload=function() {
getInfo();
}
function getInfo(){
inp=document.forms[0].getElementsByTagName('input');
for(c=0;c<inp.length;c++) {
alert('name= '+inp[c].name+' value= '+inp[c].value);
}
}
</script>
</head>
<body>
<form action="#">
<div>
<input type="text" name="foo" value="good"/>
<input type="text" name="blah" value="bad"/>
<input type="text" name="dodah" value="ugly"/>
</div>
</form>
</body>
</html>
This will work for any number of input elements and can, if necessary, be refined to select them by type.
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.