Solved

onChange not working for select element

Posted on 2011-03-08
10
545 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
replace link with jQuery 4 15
Mouse event to control image and transparency. 4 22
Else condition 9 19
innerHTML 7 23
This article discusses four methods for overlaying images in a container on a web page
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

831 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