Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-30-2010, 06:09 AM   PM User | #1
avizhome
New Coder

 
Join Date: Mar 2010
Location: Australia
Posts: 93
Thanks: 38
Thanked 0 Times in 0 Posts
avizhome is on a distinguished road
redirect to new page

Hi,
I have a form in which i have a dropdown list and a submit button. I want to code so that when users select the value from the dropdown list, it should redirect then to the specified page. For instance i have the value, "car", "House" and "pets" in my dropdown listbox, if a use click on "car" it should redirect them to the car page. I have done the following code, but something seems not to work
Code:
<script type="text/javascript">
function move(){
if (document.sell.cat.value == 'Cars'){
window.location = 'http://www.yourdomain.com'
}
</script>
and in the submit button am calling the function move()
Code:
<input type="submit" name="button2" id="button2" value="Search" onclick="move()"/>
my form name is sell and the dropdown list menu is cat.
Thanks

Last edited by avizhome; 03-30-2010 at 06:25 AM..
avizhome is offline   Reply With Quote
Old 03-30-2010, 06:56 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
You can do it much more simply than that:
Code:
<select onchange="location.href=this.value;">
<option value="#">--choose one--</option>
<option value="http://www.espn.com">Sports</option>
<option value="http://www.cnn.com">News</option>
<option value="cats.html">Kitties</option>
<option value="canines.html">Dogs</option>
</select>
Your user doesn't even need to click on a submit button. Simpler user interface.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 03-30-2010, 07:01 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Oh...and your problem is that you used a *SUBMIT* button!

So it tries to change the URL via the JavaScript code, but then the normal action of the SUBMIT button takes over and posts the <form> right back to this page.

If you would just change type="submit" to type="button" I bet it would work.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
avizhome (03-30-2010)
Old 03-30-2010, 07:09 AM   PM User | #4
avizhome
New Coder

 
Join Date: Mar 2010
Location: Australia
Posts: 93
Thanks: 38
Thanked 0 Times in 0 Posts
avizhome is on a distinguished road
awesome thanks ....both ur solution were perfect
avizhome is offline   Reply With Quote
Old 03-30-2010, 07:30 AM   PM User | #5
avizhome
New Coder

 
Join Date: Mar 2010
Location: Australia
Posts: 93
Thanks: 38
Thanked 0 Times in 0 Posts
avizhome is on a distinguished road
can we call more then a function on body load?
am trying this but it doesn't work
Code:
<body onload="MM_preloadImages('images/home_2.jpg','images/cars_2.jpg','images/real_state_2.jpg','images/computers_2.jpg','images/usp_2.jpg','images/jobs_2.jpg','images/dating_2.jpg','images/contact_2.jpg');fillCategory()">
my fillcategory function:
Code:
<script language="javascript" >
function fillCategory(){ 
addOption(document.register.country, "Fiji", "Fiji", "");
addOption(document.register.country, "Australia", "Australia", "");
addOption(document.register.country, "New Zealand", "New Zealand", "");
}
</script>
avizhome is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:47 AM.


Advertisement
Log in to turn off these ads.