Solved

onChange not working for select element

Posted on 2011-03-08
10
537 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

911 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

21 Experts available now in Live!

Get 1:1 Help Now