Page 1 of 1

Chain select

Posted: Mon Nov 14, 2011 11:42 am
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>

Re: Chain select

Posted: Tue Nov 15, 2011 7:58 am
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.

Re: Chain select

Posted: Thu Nov 17, 2011 12:44 am
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!