Chain select

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
wihvd
Forum Newbie
Posts: 6
Joined: Mon Mar 29, 2010 12:29 am

Chain select

Post by wihvd »

Hi, why function getCities() doesn't work, although first function works?

Code: Select all

<html>
<head>
<script type="text/javascript">
	function getStates(){
		var xmlhttp;
		try {
			xmlhttp = new XMLHttpRequest;
		} catch (e)
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		if (xmlhttp)
		{
			var form = document['form1'];
			var country = form['country'].value;
			
			xmlhttp.open("GET", "http://localhost/projects/jquery_chosen/harvesthq-chosen-875c800/test/test7/prototype2/getStates.php?country="+country, true);
			xmlhttp.onreadystatechange = function()
			{
				if(this.readyState == 4)
				{
					var s = document.createElement("select");
					s.name = "state";
					s.innerHTML = this.responseText;
					
					form.replaceChild(s, form['state']);
				}
			}
			xmlhttp.send(null);
		}
	}
	function getCities(){
		alert ("city");
	}
</script>
</head>
<body>
	<form name="form1" method="POST">
    	<select name="country" onChange="window.getStates()">
        	<option disabled>Select Country</option> 
            <option value="usa">USA</option>
            <option value="uk">UK</option>
        </select>
        <select name="state" onChange="window.getCities()">
        	
        </select>
        <select name="city">
        </select>
    </form>
</body>
</html>
uday8486
Forum Newbie
Posts: 22
Joined: Fri Oct 28, 2011 11:42 pm
Location: Pune, India

Re: Chain select

Post by uday8486 »

This might be because you are dynamically creating the select element. and you are not attaching you onchange event to that newly created element.
see this code var s = document.createElement("select"); your creating new states select box.

Instead of creating new dropdown, add only options to the states select box which is already there.
Enidgenevieve
Forum Newbie
Posts: 1
Joined: Mon Nov 14, 2011 3:32 am

Re: Chain select

Post by Enidgenevieve »

Link must find related sites, and for the other sites do not use black hat methods (very important oh)
* High-quality import is also very important. Namely: the site as long as possible, the original site, the higher the better. Do not simply look at PR!
* Continue to increase links, and send in the relevant forums related to soft!
Post Reply