I need javascript for automatic city display when i click one state from list box.

for example one list box i fetch karnataka, then automatically cities display whatever cities there in karnataka in another list box .

Please reply..am newbee of script writing....

Recommended Answers

All 8 Replies

I have a solution to this but it is intended for a form using select boxes... Is this what your looking for? And my solution is for counties and cities in England.

Metophase..

Give that code...that is when i click england from select box then automatically loading cities are display into another select box...

please reply soon...

<form name="location">
<select name="country" id="country" onchange="redirect(this.options.selectedIndex)">
<option></option>
<option>England</option>
<option>Scotland</option>
<option>Wales</option>
<option>Northern Ireland</option>
</select>
<select name="city" id="city">
</select>
</form>
<script language="javascript">
							var countries =document.location.country.options.length
							var country = new Array(countries)
							for (i=0; i<countries; i++)
							country[i]=new Array()

							country[0][0] = new Option("")
							country[0][1] = new Option("Please select a Country First")

							country[1][0] = new Option("")
							country[1][1] = new Option("Manchester")
							country[1][2] = new Option("London")
							country[1][3] = new Option("Birmingham")

							country[2][0] = new Option("")
							country[2][1] = new Option("Aberdeen")
							country[2][2] = new Option("Glasgow")

							country[3][0] = new Option("")
							country[3][1] = new Option("flyglwnygfly")
							country[3][2] = new Option("glyndaflygllyl")
							country[3][3] = new Option("glyfaglynglyn")

							country[4][0] = new Option("")
							country[4][1] = new Option("Londonderry")
							country[4][2] = new Option("Dublin")
							country[4][3] = new Option("Galway and Mayo")

							var temp=document.location.city

							function redirect(x){
								for (m=temp.options.length-1;m>0;m--)
									temp.options[m]=null
								for (i=0;i<country[x].length;i++){
									temp.options[i]=new Option(country[x][i].text)
								}
								temp.options[0].selected=true
							}  
						</script>

Erm maybe I should explain a bit more........ lol, you need to define all the cities associated with the country in the JavaScript... and have the countries listed in teh first select box... this can get very log winded for mine there are 47 counties and between 1-10 districts in each of the counties... but it does work very smoothly and if you add a couple more things you can cater for when a person as javascript turned off. this is the bare bones

yeah, metophase sounds right. create the array and go from there.

Thanks for sending code...
when i click england then display cities from englad... again when i click one of the cities from england automatically show whether deatils of that particular city.....
please reply

Siva, i would hate to think your are copying and pasting the code and not understanding it... before i give anything else away please think about each line of code.... there is more to learn from it than you think... just study it and see if you can understand exactly what is happening... if you do you can expand on it and learn from it... if you have a go and then send me the code i will help you further

commented: great repy bloke, patient, giving the OP the chance to teach themselves, and be able to do it nexxt time as well +13

Dear Sir,

When I select Country from first listbox it doesnot show state name in the 2nd listbox can you help me

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.