SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Select Box

  1. #1
    SitePoint Evangelist FCC's Avatar
    Join Date
    May 2006
    Posts
    417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Select Box

    Hi all,

    I am having SO MUCH trouble making this one script work:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Untitled Document</title>
    <script type="text/javascript">
    function test()
    {
    	var selectBox = document.forms[0].test;
    	user_input = selectBox.options[selectBox.selectedIndex].value
    	prompt(user_input);
    	//prompt(document.modifyForm.test.selectedIndex, "");
    	//prompt(document.modifyForm.test.options[test.selectedIndex].value);
    }
    </script>
    </head>
    
    <body>
    <form action='Javascript: test()'>
    <select name='test' >
    <option>sdfsdf</option>
    <option>sdfsdf</option>
    </select>
    <input type="submit">
    </form>
    </body>
    </html>
    Basically, I just want to choose an option and then when press submit it prompts the user_input. But like I can't get it work!

    Can anyone help me?

  2. #2
    Non-Member melancholic's Avatar
    Join Date
    Nov 2004
    Location
    Australia
    Posts
    447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's because you're not settting a value to your options.


    change this:
    HTML Code:
    <option>sdfsdf</option>
    to this:
    HTML Code:
    <option value="sdfsdf">sdfsdf</option>
    regards,

  3. #3
    Non-Member melancholic's Avatar
    Join Date
    Nov 2004
    Location
    Australia
    Posts
    447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    alternatively, you can assign the text label instead of the option value to the "user_input" temp.

    like so
    Code:
    	user_input = selectBox.options[selectBox.selectedIndex].text

  4. #4
    SitePoint Evangelist FCC's Avatar
    Join Date
    May 2006
    Posts
    417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Omg. I suck. Thanks all.

  5. #5
    Non-Member melancholic's Avatar
    Join Date
    Nov 2004
    Location
    Australia
    Posts
    447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol, don't be too hard on yourself


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
  •