SitePoint Sponsor |
|
User Tag List
Results 1 to 3 of 3
-
Oct 4, 2002, 06:51 #1
- Join Date
- Jul 2002
- Posts
- 73
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Getting Form variable from select list
Can anyone help?
I want to alter the select name part of a variable declaration using a variable i.e.
var $selectname="selectname";
var $name=document.form1.$selectname.value;
This returns an error - object null
This works fine
var $name=document.form1.selectname.value;
The problem is with 50 different select names I need to declare the variable 50 times
Any Ideas on how I might change the following code so that the options in select2 get passed to the $text variable when the second button is clicked.
David Gale
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/JavaScript">
<!--
function test(str){
$text=document.form1.select1.value;
alert (str + " " + $text);
}
//-->
</script>
</head>
<body>
<form name="form1" method="post" action="" >
<p>
<select name="select1" id="select1">
<option value="Black">Black</option>
<option value="Blue">Blue</option>
</select>
<a href="javascript:;" onClick="test('test string 2')"><img src="addbutton.gif" width="24" height="24" border="0"></a>
</p>
<p>
<select name="select2" id="select2">
<option value="Red">Red</option>
<option value="White">White</option>
</select>
<a href="javascript:;" onClick="test('test string')"><img src="addbutton.gif" width="24" height="24" border="0"></a> </p>
</form>
</body>
</html>
-
Oct 4, 2002, 10:29 #2
- Join Date
- Jul 2002
- Location
- Dallas, TX
- Posts
- 2,900
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Ok, I'm not totally positive on what you are trying to accomplish, but I can fix this reference for you...
var $name=document.form1.$selectname.value;
should be
var $name=document.form1.elements[$selectname].value;
Also, I'd avoid using the $ with variable names, I'm pretty sure they aren't valid in older JS versions (like IE4)
-
Oct 4, 2002, 15:43 #3
- Join Date
- Jul 2002
- Posts
- 73
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thank you Beetle
I have generated a shopping products list from a database. Some products have colour options and some size options and these options are selected from a drop down list. Getting the main variables into the shopping cart array was no problem but I have been tearing my hair out trying to get the size and colour options into the cart array.
Your solution is perfect for my purposes.
Thank you also for the tip regarding the $ with the variable names
David Gale
Bookmarks