Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7446267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:16:28+00:00 2026-05-29T12:16:28+00:00

I populate child dropdown (second dropdown) by the onchange event in parent dropdown (first

  • 0

I populate child dropdown (second dropdown) by the onchange event in parent dropdown (first dropdown). After that by onchange event of child dropdown I am autofilling three text boxes. But my issue is with first dropdown. Id of first dropdown is combo and id of second dropdown is combo1.

When I select A from first dropdown then I in second dropdown I have got a value 1 by appending in javascript written below in auto.jsp. But when I select option B in first dropdown then I got 2 in second dropdown but the old value 1 should be removed from second dropdown, but it is still remaining there, why? Similarly when I am selecting A or B from first dropdown multiple times then multiple values 1 or 2 are coming in second dropdown which I want to remove and to display single times that is if A will be selected then 1 will be displayed and if B will be selected then 2 will be displayed, how can it be done?

auto.jsp

   <script type="text/javascript">
  $(document).ready(function() {
 $("#combo").change(function() {// by onchange event in first dropdown I populate  second dropdown having id combo1
  $.getJSON('combo1.jsp', {firstcombobox : this.value}, function(responseData) {
   $("#combo1").append(
$("<option></option>").html(responseData.name).val(responseData.name)
);
  });
});
// After getting value in second dropdown, by onchange event i am autofilling 3 textboxes.
$("#combo1").change(function() {

 $.getJSON('combo2.jsp', { combo1Val : $(this).val() }, function(data) {

 $("#firsttextbox").val(data.name);
$("#secondtextbox").val(data.roll_no);
 $("#thirdtextbox").val(data.fine);
  });
 });
});          
    </script>
<body>
//First dropdown
 <select id="combo" name="firstcombobox">

     <option value="">select</option>
      <option value="a">A</option>
       <option value="b">B</option>
        </select> 
// Second dropdown
      <select id="combo1" name="combo1Val" >
     <option value="">select</option>
 </select> 
</body>

combo1.jsp

<%@page import="net.sf.json.JSONObject"%>
<%@page import="net.sf.json.JSONArray"%>
<%
String firstcombobox=request.getParameter("firstcombobox");

if(firstcombobox.equalsIgnoreCase("a")){// If selected value in first dropdown is A then 1 will be displayed as written below
JSONObject arrayObj= new JSONObject();

       arrayObj.put("name","1");// I displayed 1 in second dropdown option when A is selected
          response.setContentType("application/json");
      response.getWriter().write(arrayObj.toString());
      }
else if(firstcombobox.equalsIgnoreCase("b")){
  JSONObject arrayObj= new JSONObject();
       arrayObj.put("name","2");
          response.setContentType("application/json");
      response.getWriter().write(arrayObj.toString());
}
else{
}
%>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T12:16:32+00:00Added an answer on May 29, 2026 at 12:16 pm

    In auto.jsp ‘#combo’ change anonymous function, replace:

    $.getJSON('combo1.jsp', {firstcombobox : this.value}, function(responseData) {
        $("#combo1").append(
            $("<option></option>").html(responseData.name).val(responseData.name)
        );
    });
    

    with:

    $.getJSON('combo1.jsp', {firstcombobox : this.value}, function(responseData) {
        $("#combo1").empty().append(
            $("<option></option>").html(responseData.name).val(responseData.name)
        );
    });
    

    To split the string into an array look here: How do I split a string, breaking at a particular character?

    They use as follows:

    $.getJSON('combo1.jsp', {firstcombobox : this.value}, function(responseData) {
        var splitValues = responseData.name.split(/,/);
    
        $("#combo1").empty().append("<option value="0">Please select...</option>");
    
        for (var idx in splitValues) {
            $("#combo1").append(
                $("<option></option>").html(splitValues[idx]).val(splitValues[idx])
            );
        }
    });
    

    Hope this helps?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After writing code to populate textboxes from an object, such as: txtFirstName.Text = customer.FirstName;
I have Parent page and a Child page('empW.cfm'),which is nothing but a popup window
I am working on a stored proc (parent) that calls another stored proc (child).
I have a WinForm TreeView Control that displays the Parent Child relationship of CaseNotes(I
I'm using jQuery treeview plugin to populate a child treeview once click a folder
I would like to know the best way to populate an object that has
I'm writing a bit of code that I want to populate a TreeView, which
I have some models all linked together in memory (parent:child:child:child) and saved at the
We have nested folders with parent-child relationship. We use MySQL MyISAM DB. The data
I have a parent class which holds a map and n the child class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.