Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-09-2012, 08:37 AM   PM User | #1
msurya
New to the CF scene

 
Join Date: Oct 2012
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
msurya is an unknown quantity at this point
Country state dropdown using javascript and jsp

Hi,
I am a fresher, started my career in java, learning javascript. I have coded a registration form where I have a dropdown of all the countries from the database, and based on that the states corresponding to that country have to get populated.I googled many forums and finally have done it using ajax. I also wanted to do it in javascript .Can someone help me out.
msurya is offline   Reply With Quote
Old 10-09-2012, 08:39 AM   PM User | #2
shyagrawal
New Coder

 
Join Date: Sep 2012
Posts: 22
Thanks: 0
Thanked 6 Times in 6 Posts
shyagrawal is an unknown quantity at this point
Hi,

You can call onchange function on Country Select tag. Than in that function, you can set another select tag value.
shyagrawal is offline   Reply With Quote
Users who have thanked shyagrawal for this post:
msurya (10-09-2012)
Old 10-09-2012, 08:49 AM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Is this what you are wanting?

Code:
<html>
<head>

<script type="text/javascript">

var countries = [ ];
countries["Egypt"]   = ["Cairo","Alexandria","Sharm","Luxor"];
countries["Germany"] = ["Munchen","Dortmund","Berlin","Koln", "Nurnberg"];
countries["France"] = ["Paris", "Rouen", "Lille", "Lyon", "Marseilles"];
countries["USA"]     = ["New York","Chicago","Dallas","Seattle","Los Angeles", "Atlanta", "Boston", "Miami"];
countries["Japan"]   = ["Tokyo","Osaka","Kyoto","Sapporo"];

function switchCountry(selCountry) {
var citySel = selCountry.form.City;
for ( var s = citySel.options.length-1; s > 0; --s )  {
citySel.options[s] = null;
}

var chosen = selCountry.options[selCountry.selectedIndex].text;
var cList = countries[chosen];
if ( cList != null )   {
for ( var i = 0; i < cList.length; ++i )   {
citySel.options[i+1] = new Option(cList[i],cList[i]);
}
}

}

function showSelection() {
var country = document.forms[0].Country.value;
var city = document.forms[0].City.value;
if (city != "") {
alert ("You selected " + city + " in " + country);
}
}

</script>

</head>
<body>
<form>
<select name="Country" onchange="switchCountry(this);">
<option value = "">Choose Country</option>
<option value = "Egypt">Egypt</option>
<option value = "Germany">Germany</option>
<option value = "France">France</option>
<option value = "Japan">Japan</option>
<option value = "USA">USA</option>
</select>
&nbsp;&nbsp;&nbsp;&nbsp;
<select name="City" onchange = "showSelection()">
<option>Choose City</option>
</select>
</form>
</body>
</html>

Quizmaster: Which Russian leader is credited as the architect of glasnost?
Contestant: Stalin
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
msurya (10-09-2012)
Old 10-09-2012, 01:56 PM   PM User | #4
msurya
New to the CF scene

 
Join Date: Oct 2012
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
msurya is an unknown quantity at this point
Thanks for the reply.I have tried the same, giving the <script> in .js file. I want to get the list of countries given in the <option> tag through MVC.

Last edited by msurya; 10-11-2012 at 10:41 AM.. Reason: I dint know that it should be in # as am new to CF.
msurya is offline   Reply With Quote
Old 10-09-2012, 01:57 PM   PM User | #5
msurya
New to the CF scene

 
Join Date: Oct 2012
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
msurya is an unknown quantity at this point
Thanks for the reply.I have tried the same, giving the <script> in .js file. I want to get the list of countries given in the <option> tag through MVC.

Country:
<td><form:select path="countryId" id="cld" name="cld" items="${countryList}" onchange="statesSelected();">
<formption value="-1" label="--- Select ---"/></form:select></td>

State:
<td><form:select path="stateId" id="sId"><formption value="-1" label="--- Select ---"/> </form:select></td>
<td id="stateError" style="margin-left:120px;color:red;"></td>[/QUOTE]
msurya is offline   Reply With Quote
Old 10-09-2012, 03:30 PM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
When posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. It should be obvious why this is necessary This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 10-10-2012, 06:11 AM   PM User | #7
msurya
New to the CF scene

 
Join Date: Oct 2012
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
msurya is an unknown quantity at this point
Sorry, I dint know that it should be in # as am new to CF. And please check the edited code.
msurya is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:53 PM.


Advertisement
Log in to turn off these ads.