954,566 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to make javascript form autochange - "Onchange"?

Hello, I have this code:

<select name="product" class="buy">
                  <option value="products.php" selected>Select Term</option>
                  <option value="products.php?item=1&term=1">2 Month</option>
                  <option value="products.php?item=1&term=3">3 Months</option>
                  <option value="products.php?item=1&term=6">4 Months</option>
                  <option value="products.php?item=1&term=12">5 Months </option>
                </select>


I need to make it so as soon as you click that select box, it loads the url (value)... if anyone can help me that would be great!

Philz
Newbie Poster
3 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

Loads it where?

Look at some of the code in this article: Creating an HTML "ComboBox" control

It demonstrates all the techniques you'll need to reference Select/Option controls, retrieve their values, and copy them into other controls.

tgreer
Made Her Cry
Team Colleague
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37
 

Thanks for your help.Here's my code again:

<select name="product" class="buy">
                  <option value="products.php" selected>Select Term</option>
                  <option value="products.php?item=1&term=1">2 Month</option>
                  <option value="products.php?item=1&term=3">3 Months</option>
                  <option value="products.php?item=1&term=6">4 Months</option>
                  <option value="products.php?item=1&term=12">5 Months </option>
                </select>


When they click the "2 Month" value, it will load http://www.mydomain.com/products.php?item=1&term=1 . Do you see what I'm trying to do? Thanks

Philz
Newbie Poster
3 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

Sure. Add on onChange handler:

<select name = "product" class="buy" onChange="JavaScript:mySelect(this);">


Code the mySelect function in the head/script section:

function mySelect(x)
{
  window.location.href = "http://www.your_domain.com/"  +   
     x.options[x.selectedIndex].value;
}


Some variation of that ought to do the trick.

tgreer
Made Her Cry
Team Colleague
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You