Solved

onChange not working for select element

Posted on 2011-03-08
10
552 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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
 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

830 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