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

  • Home
  • SEARCH
  • 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 6598433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:20:54+00:00 2026-05-25T18:20:54+00:00

I have one drop-down list (<select> <option>) on page with id="coclevel1" . If current

  • 0

I have one drop-down list (<select> <option>) on page with id="coclevel1". If current selected option has child items then I have to add new drop-down list and fill it with child items. Next drop-down lists should named like "coclevel2", "coclevel3", etc. I’ve to do this event for every added drop-down list. In $(document).ready I placed:

$('[id^=coclevel]').live('change', function() {
    var currElement = $(this).attr('id');
    alert(currElement); //for debug
    cleanUpCocOptionsAfter(currElement);
    renderSubItemsOf(currElement);
});

renderSubItemsOf(currElement); make query to DB, and if data is not empty add new drop-down list and fill it.

function cleanUpCocOptionsAfter(currElement) {
    var num = $('.cocItems').length;
    var currNum = parseInt(currElement.substring(8, currElement.length));
    for (var i = currNum + 1; i <= num; i++) {
        $('#coclevel' + i).remove();
        $('#cocBr' + i).remove();
    }
}

function renderSubItemsOf(currElement) {
    var parentId = $('#' + currElement + ' option:selected').val();
    $.getJSON('getchildrenof/' + parentId,
            function() {

            }).success(function(data) {
                var len = data.length;
                if (len > 0) {
                    var newElemSelector = '#' + addCocOptionAfter(currElement);
                    for (var i = 0; i < len; i++) {
                        $(newElemSelector).append($("<option></option>").attr("value", data[i].id).text(data[i].description));
                    }
                } else {
                    //not developed yet
                }
            });
}

function addCocOptionAfter(currElement) {
    // how many "duplicatable" input fields we currently have
    var num = $('.cocItems').length;
    var currNum = parseInt(currElement.substring(8, currElement.length));
    var currElemSelector = '#' + currElement;
    // the numeric ID of the new input field being added
    var newNum = new Number(num + 1);

    // create the new element via clone(),
    //and manipulate it's ID using newNum value
    var newElemId = 'coclevel' + newNum;

    var newElem = $(currElemSelector).clone().attr('id', newElemId).empty();
    $(newElem).append($("<option></option>").attr("value", -1).text('-- Select one --'));

    // insert the new element after the last "duplicatable" input field
    $(currElemSelector).after(newElem);
    $(currElemSelector).after('<br/>').attr('id', 'cocBr' + newNum);

    return newElemId;
}

Adding new drop-down box and filling works fine, but thereafter previous drop-down box lost event, and when I try to change selected option nothing appears (browser don’t show alert(currElement); //for debug). Please help to solve this issue.

  • 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-25T18:20:55+00:00Added an answer on May 25, 2026 at 6:20 pm

    You don’t need to be using numeric ids here. You should be able to do this more efficiently by passing around the DOM objects themselves:

    $('.cocItems').live('change', function() {
        cleanUpCocOptionsAfter(this);
        renderSubItemsOf(this);
    });
    
    function cleanUpCocOptionsAfter(currElement) {
        $(currElement).nextAll('.cocItems').remove();
    }
    
    function renderSubItemsOf(currElement) {
        var parentId = $(currElement).val();
        $.getJSON('getchildrenof/' + parentId, function(data) {
            var len = data.length;
            if (len > 0) {
                addCocOptionAfter(currElement, data)
            } else {
                //not developed yet
            }
        });
    }
    
    function addCocOptionAfter(currElement, data) {
        var newElem = $('<select />').addClass('cocItems');
        $('<option />')
            .attr('value', -1)
            .text('-- Select one --')
            .appendTo(newElem);
    
        $.each(data, function() {
             $('<option />')
                 .attr('value', this.id)
                 .text(this.description)
                 .appendTo(newElem);
        });
        newElem.insertAfter(currElement);
        return newElem;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two drop down lists: <select name="branch"> <option value="b">Blacksburg</option> <option value="c">Christiansburg</option> <option value="f">Floyd</option>
I want to add a Select One option to a drop down list bound
Suppose I have a drop-down list like: <select id='list'> <option value='1'>Option A</option> <option value='2'>Option
in another words, i have an drop-down list: <select name=gamelist id=gamelist> <option value=1>Backgammon</option> <option
Here's the situation I have a webpage which has one drop down called prefer.
I have a form with two text boxes, one select drop down and one
I have the following HTML containing a drop down list (single selection) <script type="text/javascript">
I have a drop down list's select tag that looks like this: <select name=MyName
I have the following drop down list and selects the option based on the
I have this application that has a form with a drop-down list and a

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.