0

I have a button that appends a select every-time I press it with an increasing id

The function below should be fired when I change one of the selects but it does only when I change the first.

Here is the .onchange function

     var appendedCheckboxes =[];
        $("select[id^='catalogName']").change(function(event){
            debugger;
            var cataid = $("select[id*=catalogName").val()

            var cata = out.find(element => {
                return element._id == parseInt(cataid)
            })
            var catalogId = event.target.id.slice(-1);

            if(jQuery.inArray("#insertCheboxesForCatalogsHere"+catalogId, appendedCheckboxes) !== -1){
                $("#insertCheboxesForCatalogsHere" + catalogId + " div").last().remove();
                appendedCheckboxes.splice(jQuery.inArray("#insertCheboxesForCatalogsHere"+catalogId, 1));
            }
            var count = 1

            cata.categories.forEach(element => {
                $('#insertCheboxesForCatalogsHere' + catalogId).append('<div class="input-group mb-1"><div style="margin-left: 10px" class="custom-control custom-checkbox my-1 mr-sm-2" id="task' + count + '"><input type="checkbox" class="custom-control-input" id="' + cata._id + "," + element._id + '"><label class="custom-control-label" for="' + cata._id + "," + element._id + '">'+element.name +'</label></div></div>')            
                count++;
            })
            appendedCheckboxes.push("#insertCheboxesForCatalogsHere" + catalogId);
        })

The selects I append

 $('#insertCatalogHere').append(
'<div id="insertCheboxesForCatalogsHere' + catalogcounteradd +'"class="input-group mb-1">
<div class="input-group-prepend">
<span class="input-group-text" id="">Catalog ' + catalogcounteradd + '</span></div>
<select class="custom-select" id="catalogName' + catalogcounteradd + '" name="catalogName' + catalogcounteradd + '" required>
<option value="" selected>Catalogs...</option>
{{#each catalog}}
<option value="{{this._id}}">{{ this.name }}
</option>
{{/each}}
</select></div>');
3
  • use $("select[id*='catalogName']") Commented Aug 24, 2018 at 10:25
  • change the onchange event to like this and check if it works, $(document).on("change", "select[id^='catalogName']", function () { }) Commented Aug 24, 2018 at 10:37
  • thank you @Dhiren it works now Commented Aug 24, 2018 at 10:48

1 Answer 1

1

$(document).on("change", "select[id^='catalogName']", function () { }) works for me

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.