Please help me with this simple form script, I'm having trouble getting it to work

Discussion in 'JavaScript' started by Braydo25, Nov 19, 2009.

  1. #1
    Hello, I am trying to get this chunk of javascript to make the users browser take them to a new url depending on what option they choose in the HTML form. The full webpage code is below. The function used to process the users selection is function brandsearch(). The script does not want to work when the following lines of code are in the function (The codes that are making it not work are the ones I need to get the string value of the users selection.)

    THE FULL WEBPAGE WITH JAVASCRIPT AND FORM IS LOCATED BELOW

    var selectedindex = oForm.elements["brand"].selectedIndex;
    var selectedoptionvalue = oForm.elements["brand"].options[selected_index].value;
    var selectedoptiontext = oForm.elements["brand"].options[selected_index].text;
    Code (markup):
    if you see anything that would make it not work, please let me know. :)




    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    
    
    
    <script type="text/javascript">
    
    
    
    function brandsearch() {
    var selectedindex = oForm.elements["brand"].selectedIndex;
    var selectedoptionvalue = oForm.elements["brand"].options[selected_index].value;
    var selectedoptiontext = oForm.elements["brand"].options[selected_index].text;
    var submitto = "http://www.momentumautoworksstore.com/products/Default.aspx?brand=" + seletedoptiontext;
    window.location.href = submitto;
    return false;
    
    }
    
    
    </script>
    
    
    
    </head>
    
    <body>
    
    
    <form method="post" action="javascript:brandsearch()">
    
    <select name="brand">	
    <option selected="selected" value="">Select Brand</option>
    
    <option value="14" >AC AutoTechnic</option>
    <option value="23">AEM</option>
    <option value="47">Apex'i</option>
    <option value="5002">APR Performance</option>
    <option value="56">ARP</option>
    <option value="104">Brembo</option>
    <option value="134">Cosworth</option>
    <option value="147">DC Sports</option>
    <option value="165">Edelbrock</option>
    
    <option value="184">Extreme Dimensions</option>
    <option value="201">Fujita</option>
    <option value="217">GReddy</option>
    <option value="250">Injen</option>
    <option value="262">JSP</option>
    <option value="263">K Sport</option>
    <option value="5006">Kelford Cams</option>
    <option value="280">MagnaFlow</option>
    <option value="294">MimoUSA</option>
    
    <option value="292">Mishimoto</option>
    <option value="5004">Momentum Auto Works</option>
    <option value="320">Pacesetter</option>
    <option value="337">Pro Pods</option>
    <option value="364">Remus</option>
    <option value="381">Seibon</option>
    <option value="5003">Torque Solution</option>
    <option value="456">Weapon R</option>
    <option value="472">Yonaka</option>
    </select>
    
    <input value="Shop" type="submit" />
    
    </form>
    
    </td>
    
    </body>
    </html>
    
    HTML:
     
    Last edited: Nov 19, 2009
    Braydo25, Nov 19, 2009 IP
  2. mps705

    mps705 Peon

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Put 'oForm' as te name of your form

    <form method="post" action="javascript:brandsearch()" name="oForm">


    Try this as your function:

    function brandsearch() {
    var theBrand = document.oForm.elements["brand"];
    var selectedindex = theBrand.selectedIndex;
    var selectedoptionvalue = theBrand.options[selectedindex].value;
    var selectedoptiontext = theBrand.options[selectedindex].text;
    var submitto = "http://www.momentumautoworksstore.com/products/Default.aspx?brand=" + selectedoptiontext;
    window.location.href = submitto;
    return false;

    }
     
    mps705, Nov 19, 2009 IP
  3. Braydo25

    Braydo25 Active Member

    Messages:
    144
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    Thank you, but I actually got it working perfect another way.
     
    Braydo25, Nov 21, 2009 IP