Search code examples
htmlhttp-redirectonclicklistboxhtml-select

Use Value of Selected Item in List to Redirect Browser Accordingly [HTML]


Suppose I have a list box with different items, each with unique values, and an 'Enter' button below it. I want to be able to get the value of the selected item at the time of click, and then when have the button property:

ONCLICK="window.location.href='http://somewebsite.com/somefile.php?id="thisvalue"'"

So for example, something like----

<SELECT NAME = ParticipantList STYLE = "WIDTH: 187" SIZE = 18>
    <OPTION VALUE='hi'> hello </OPTION>
<SELECT>
<INPUT TYPE="submit" VALUE="Info" ONCLICK="window.location.href='http://helloworld.com/this.php?="hi"'"/> 

Can anyone help me figure this out? Much appreciated.


Solution

  • HTML forms are designed to do exactly this when their method is GET:

    <form action="http://helloworld.com/this.php" method="get">
      <select name="ParticipantList">
        <option value="hi">Hello</option>
      </select>
    
      <input type="submit">
    </form>
    

    This will send the user to http://helloworld.com/this.php?ParticipantList=hi. No JavaScript required.