Solved

onChange not working for select element

Posted on 2011-03-08
10
577 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
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 count occurrences of each item in an array.
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)

626 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