User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 374,027 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,897 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 699 | Replies: 1
Reply
Join Date: Aug 2007
Posts: 40
Reputation: mimsc is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
mimsc mimsc is offline Offline
Light Poster

new to JAVASCRIPT

  #1  
Sep 7th, 2007
Hi I want to populate my second drop-down menu based off of the value selected on my first drop down. Is there a function or a way of doin g this...

P.S my code is in a JSP:

  1. <%
  2. Vector theStates = WeatherDAO.getWeatherStates();
  3. %>
  4.  
  5.  
  6. <html>
  7. <head>
  8. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  9. <title>Insert title here</title>
  10. </head>
  11. <body>
  12.  
  13.  
  14.  
  15. <TR bgColor="#ffffff"><TD COLSPAN="2">
  16. <TABLE CELLPADDING="3" CELLSPACING="2">
  17. <TR><TD>
  18.  
  19. </FONT></CENTER><BR>
  20. <BR>
  21. <TABLE BORDER="0" CELLSPACING="5" CELLPADDING="5" WIDTH="100%">
  22. <TR BGCOLOR="#C0C0C0" ><TD COLSPAN="2" ALIGN="CENTER"> <FONT SIZE=+3><B> Weather Services</B></FONT>
  23. </TD></TR>
  24. </TABLE>
  25. <HR SIZE="3" NOSHADE>
  26. <font size=+1>Select the State and City for which you would like to receive weather information.</font><br>
  27. <TR><TD>
  28. <center>
  29.  
  30.  
  31. <label for="weatherStat">State:</label>
  32.  
  33. <select name="weatherStat" id="States">
  34. <% for (int i=0; i< theStates.size(); i++) { %>
  35.  
  36. <OPTION VALUE=<%=theStates%>> <%=theStates.elementAt(i)%> </OPTION>
  37.  
  38. <%
  39. }
  40. %>
  41. </select>
  42.  
  43. >
  44.  
  45.  
  46. </TD></TR>
  47. </center>
  48. <TR><TD>
  49. <br>
  50. <br>
  51. <center>
  52.  
  53. <label for="weatherCity">City:</label>
  54. <select name="weatherCity" id="Cities">
  55. <%
  56.  
  57.  
  58. Vector theCities = WeatherDAO.getWeatherCities();// <<this is wher I need to pass the selected value
  59.  
  60. %>
  61. <OPTION VALUE= </OPTION>
  62.  
  63. <%
  64. }
  65. %>
  66.  
  67.  
  68.  
  69.  
  70. </select></TD></TR>
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2007
Posts: 6
Reputation: tommybobby is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
tommybobby tommybobby is offline Offline
Newbie Poster

Re: new to JAVASCRIPT

  #2  
Dec 3rd, 2007
What you need to do the following:

1. Add the onchange event attribute to the 1st select tag

<select name="weatherStat" id="States" onchange="addNewValue();">



2. Create a javascript function called addNewValue for example.

Note: Place in the <head></head> tags


function addNewValue() {
     
     $('Cities').options[0] = new Option($('States').value, 0);

}


Ok, whats happening here, is that when an item is selected in your first select States, the onchange attribute event is fired, which in turn calls our addNewValue() javascript function. By the way, the $('Cities') notation is the same as document.getElementById.....


In the above example, for simplicity reasons , we are just adding the selected value to the first element of the Cities select box.
Last edited by tommybobby : Dec 3rd, 2007 at 12:51 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb JavaScript / DHTML / AJAX Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 11:33 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC