Search code examples
jqueryeventsclone

Cloned Select Fields do not trigger an event


I am unable to find a solution for this Table that I am cloning with JQuery. I am able to clone the table and update the ID and NAME properties to avoid html conflicts. The Select drop-down only triggers an event to its JQuery when I use the first (original) copy of the table. However, the cloned version does not trigger the required event that I am using to fill-in the Description Quantity and Price fields. I am posting my Table and JQuery. Can someone please let me know what I am missing to get this to work? I tested the JQuery by creating two tables with their respective Unique IDs that I use to trigger the event, which works fine. Then, when I use JQuery to clone the Table, the Event is not triggered.

            <table>
                <tr>
                    <td style="width:20%;">
                        <div class="input-group date">
                            <div class="input-group-addon">
                                <span class="glyphicon glyphicon-duplicate addsection"></span>
                            </div>
                            <select class="form-control" id="ProductCode1" name="ProductCode1" style="border-bottom-color:black!important;border-color:white;">
                                <option selected>Select Product</option>
                                <option value='292' data-description='APRIL Afternoon At the Greenhouse TICKET' data-price='35.00'>AAATG</option>
                                <option value='285' data-description='APRIL AFTERNOON AT THE GREENHOUSE Ticket EARLYBIRD SPECIAL' data-price='30.00'>AAATGearlybird</option>
                                <option value='30' data-description='Organic Adzuki Beans 1/2 Lb' data-price='4.25'>AB12LB</option>
                                <option value='31' data-description='Organic Adzuki Beans 1/4 Lb' data-price='2.25'>AB14LB</option>
                                <option value='29' data-description='Organic Adzuki Beans 1 Lb' data-price='8.00'>AB1LB</option>
                                <option value='47' data-description='Organic Adzuki Seeds 1 Lb' data-price='5.00'>ABS1LB</option>
                                <option value='145' data-description='Alberto&#x27;s Lemon/Garlic Salad Dressing 16 Oz.' data-price='11.00'>Alberto&#x27;s Dressing</option>
                                <option value='241' data-description='Organic Fenugreek Sprouted Beans 4 oz.' data-price='2.00'>APRIL</option>
                                <option value='310' data-description='Organic Fenugreek Sprouted Beans 16 oz' data-price='7.75'>April16oz</option>
                                <option value='309' data-description='Organic Fenugreek Sprouted Beans 8 oz' data-price='4.00'>April8oz</option>
                                <option value='284' data-description='SeaVeggieSpecialAUGUST1Kelp/1Dulse/1TripleBlendPACKAGE of 3' data-price='11.50'>AUGUST</option>
                                <option value='271' data-description='DM Organic Tomato Basil Sauce CASE' data-price='36.00'>BB BasilS case</option>
                                <option value='273' data-description='DM Organic Tomato Chili Sauce CASE' data-price='36.00'>BB ChiliS case</option>
                                <option value='276' data-description='DM Organic Linguine Pasta CASE' data-price='36.00'>BB Lpasta case</option>
                                <option value='277' data-description='DM Organic Extra Virgin Olive Oil 500ML CASE' data-price='84.00'>BB OO 500ML</option>
                                <option value='278' data-description='DM Organic Extra Virgin Olive Oil 750ML CASE' data-price='132.00'>BB OO 750ML</option>
                                <option value='275' data-description='DM Organic Spaghetti Pasta CASE' data-price='36.00'>BB Spasta case</option>
                                <option value='274' data-description='DM Organic Tagliatelle Pasta CASE' data-price='36.00'>BB Tpasta case</option>
                                <option value='272' data-description='DM Organic Tomato Vegetable Sauce CASE' data-price='36.00'>BB VegS case</option>
                                <option value='117' data-description='Organic Mixed Beans 1 Lb' data-price='8.00'>BEEMB</option>
                            </select>
                        </div>
                    <td style="width:70%;"><input id="ProductDesc1" class="form-control" style="width: 100%; border-bottom-color: black!important; border-color: white;" name="ProductDesc1" type="text" value="" /></td>
                    <td><input id="quantity1" class="form-control" style="width: 50px; border-bottom-color: black!important; border-color: white; text-align: center;" name="quantity1" type="text" value="0" placeholder="1" /></td>
                    <td>
                        <input id="ProductPrice1" class="form-control cost" style="width: 95px; text-align: center; border-bottom-color: black!important; border-color: white;" name="ProductPrice1" type="text" value="" placeholder="$0.00" />
                    </td>
                    <td>
                        <div class="input-group-addon">
                            <span class="glyphicon glyphicon-eject remove"></span>
                        </div>
                    </td>
                </tr>
            </table>

The JQuery that performs the clone and updates the ID and NAME properties:

<script>
            var template = $('#sections .section:first').clone();
            var sectionsCount = 1;
            $('body').on('click', '.addsection', function () {
                sectionsCount++;
                var section = template.clone().find(':input').each(function () {
                    var newId = this.id.substring(0, this.id.length - 1) + sectionsCount;
                    var newName = this.name.substring(0, this.name.length - 1) + sectionsCount;
                    this.id = newId; 
                    this.name = newName;
                }).end()
                    .appendTo('#sections');
                return false;
            });

            //remove section
            $('#sections').on('click', '.remove', function () {
                $(this).parent().fadeOut(300, function () {
                    $(this).parent().parent().empty();
                    return false;
                });
                return false;
            });
    </script>

Finally, the JQuery that listen to the Event to fill-in the remaining fields when I select a Product.

    $(document).ready(function () {

        $('#ProductCode1').on("change", function () { alert("It Works!") });
        $('#ProductCode2').on("change", function () { alert("It Works again!") });
        $('#ProductCode3').on("change", function () { alert("It Works a third time!") });
                    }); 
    </script>

Solution

  • jQuery code inside $(document).ready() function will work only for available DOM elements when page loaded

    So in order to make it work for dynamically cloned dropdowns update your jQuery binding code like this:

    This:

    $(document).ready(function () {
        $('#ProductCode1').on("change", function () { alert("It Works!") });
        $('#ProductCode2').on("change", function () { alert("It Works again!") });
        $('#ProductCode3').on("change", function () { alert("It Works a third time!") });
    }); 
    

    To This:

    $(document).on('change', '#ProductCode1', function(){ alert("It Works!") });
    $(document).on('change', '#ProductCode2', function(){ alert("It Works again!") });
    $(document).on('change', '#ProductCode3', function(){ alert("It Works a third time!") });