SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Enthusiast
    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>

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    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)
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    SitePoint Enthusiast
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •