Solved

onChange not working for select element

Posted on 2011-03-08
10
531 Views
Last Modified: 2012-05-11
I have the following form that is ignoring the onChange event for Bonita Springs when it is selected...what am I doing wrong?

<form style="margin:0px;" action="/lee-county/residential?frame=RESI" method="post">
<table width="100%" border="0" cellspacing="0" cellpadding="3">
    <tr>
      <td><div style="padding-bottom:4px;">City: <select name="search_city">
        <option value="Alva">Alva</option>
        <option value="Bonita Springs" onChange="this.form.action='?frame=RESI&base=frames-collier';" >Bonita Springs</option>  
      </select></div>                   
<input type="image" id="WADbSearch1" name="WADbSearch1" src="/images/search.png" value="Submit">
		</form>                                                            
                        </td>
    </tr>
  </table>	
</form>	

Open in new window

0
Comment
Question by:pda4me
  • 5
  • 4
10 Comments
 
LVL 4

Expert Comment

by:WhiteSeed
ID: 35071868
onchange should not be placed in the value but in the select
<form style="margin:0px;" action="/lee-county/residential?frame=RESI" method="post">
<table width="100%" border="0" cellspacing="0" cellpadding="3">
    <tr>
      <td><div style="padding-bottom:4px;">City: <select name="search_city">
        <option value="Alva">Alva</option>
        <option value="Bonita Springs" onChange="this.form.action='?frame=RESI&base=frames-collier';" >Bonita Springs</option>  
      </select></div>                   
<input type="image" id="WADbSearch1" name="WADbSearch1" src="/images/search.png" value="Submit">
		</form>                                                            
                        </td>
    </tr>
  </table>	
</form>	

Open in new window

0
 
LVL 4

Expert Comment

by:WhiteSeed
ID: 35071880
sorry i've paste the wrong code, this is the rigth code:

<form style="margin:0px;" action="/lee-county/residential?frame=RESI" method="post">
<table width="100%" border="0" cellspacing="0" cellpadding="3">
    <tr>
      <td><div style="padding-bottom:4px;">City: <select name="search_city" onChange=="this.form.action='?frame=RESI&base=frames-collier';" >
        <option value="Alva">Alva</option>
        <option value="Bonita Springs" >Bonita Springs</option>
      </select></div>                   
<input type="image" id="WADbSearch1" name="WADbSearch1" src="/images/search.png" value="Submit">
		</form>                                                            
                        </td>
    </tr>
  </table>	
</form>

Open in new window

0
 

Author Comment

by:pda4me
ID: 35071896
But I need the action to change based on the option chosen from the select, how do I do this?
0
 

Author Comment

by:pda4me
ID: 35071920
Specifically, the <option will have diffrent onChange like this sample:

<form style="margin:0px;" action="/lee-county/residential?frame=RESI" method="post">
<table width="100%" border="0" cellspacing="0" cellpadding="3">
    <tr>
      <td><div style="padding-bottom:4px;">City: <select name="search_city">
        <option value="Alva">Alva</option>
        <option value="Bonita Springs" onChange="this.form.action='?frame=RESI&base=frames-collier';" >Bonita Springs</option>  
        <option value="Another" onChange="this.form.action='?frame=LOTL&base=frames-sanibel';" >Another</option>  
      </select></div>                   
<input type="image" id="WADbSearch1" name="WADbSearch1" src="/images/search.png" value="Submit">
		</form>                                                            
                        </td>
    </tr>
  </table>	
</form>

Open in new window

     
0
 
LVL 4

Expert Comment

by:WhiteSeed
ID: 35071978
you can use the if statement
        onChange="if(this.options[1].selected) alert('Hai selezionato il secondo'); else if(this.options[2].selected) alert('Hai selezionato il terzo') ">

Open in new window

0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:pda4me
ID: 35072010
Thanks but I am having trouble following your example.  Can you please put it into the context of the form provided?
0
 
LVL 4

Expert Comment

by:WhiteSeed
ID: 35072087
ok, sorry :

<form style="margin:0px;" action="/lee-county/residential?frame=RESI" method="post">
<table width="100%" border="0" cellspacing="0" cellpadding="3">
    <tr>
      <td><div style="padding-bottom:4px;">City: <select name="search_city" onChange=" if(this.options[1].selected) this.form.action='?frame=RESI&base=frames-collier' if(this.options[2].selected) this.form.action='?frame=LOTL&base=frames-sanibel';" >
        <option value="Alva">Alva</option>
        <option value="Bonita Springs"  >Bonita Springs</option>
        <option value="Another" >Another</option>
      </select></div>
<input type="image" id="WADbSearch1" name="WADbSearch1" src="/images/search.png" value="Submit">
		</form>                                                            
                        </td>
    </tr>

Open in new window

 </table>      
</form>
0
 
LVL 4

Assisted Solution

by:WhiteSeed
WhiteSeed earned 100 total points
ID: 35072137
i can't try your example, but write the code like this:

<form style="margin:0px;" action="/lee-county/residential?frame=RESI" method="post">
<table width="100%" border="0" cellspacing="0" cellpadding="3">
    <tr>
      <td><div style="padding-bottom:4px;">City: <select name="search_city" onChange="  alert('ISTRUCTION1');alert('INCSTRUCTION2')" >
        <option value="Alva">Alva</option>
        <option value="Bonita Springs"  >Bonita Springs</option>
        <option value="Another" >Another</option>
      </select></div>
<input type="image" id="WADbSearch1" name="WADbSearch1" src="/images/search.png" value="Submit">
		</form>                                                            
                        </td>
    </tr>
  </table>	
</form>

Open in new window

0
 
LVL 82

Accepted Solution

by:
hielo earned 400 total points
ID: 35077573
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
<script type="text/javascript">
function handleChange( sel )
{
	var theForm=sel.form;
	
	switch( sel.value )
	{
		case 'Bonita Springs':
			theForm.action='?frame=RESI&base=frames-collier';
			break;

		case 'Another':
			theForm.action='?frame=LOTL&base=frames-sanibel';
			break;

		default:
			theForm.action="/lee-county/residential?frame=RESI"
			break;
	}
	//uncomment the following so you can see the value in the form's "action"
	//alert( theForm.action )
}
</script>
</head>
<body>
<form style="margin:0px;" action="/lee-county/residential?frame=RESI" method="post">
<table width="100%" border="0" cellspacing="0" cellpadding="3">
    <tr>
      <td><div style="padding-bottom:4px;">City: <select name="search_city" onchange="handleChange(this)">
													<option value="Alva">Alva</option>
													<option value="Bonita Springs">Bonita Springs</option> 
													<option value="Another">Another</option> 
												</select>
			</div>
			<input type="image" id="WADbSearch1" name="WADbSearch1" src="/images/search.png" value="Submit">
		</form>                                                            
                        </td>
    </tr>
  </table>	
</form>	
</body>
</html>

Open in new window

0
 

Author Closing Comment

by:pda4me
ID: 35083784
Thanks, the extra javascript worked properly!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now