1

I have created a dynamic table where by a click of a button it will generate a new row every time the button is pressed. Everything works as it supposed to but the class attribute in the select tag or any other input does not work properly. How do I get it to work. Any Idea??

Here is the .js code that works and adds new row on button click:

        $('.plusbtn12').click(function () {
            $(".quoteTable12").append('<tr><td><select class="js-select2 form-control c_solutions" name="i_solution[]" style="width: 100%;" data-placeholder="Choose one.."></select></td><td class="hidden-xs"><input class="form-control" type="text" name="i_quantity[]" placeholder="Quantity"></td><td><input class="form-control c_costing" type="text" name="i_cost[]" placeholder="Cost"></td></tr>');

            var options = "";

            $.getJSON("getSolution12.php", function (data) {

                for (i = 0; i < data.length; i++) {
                    options += "<option>" + data[i] + "</option>";
                }

                $('.quoteTable12 tr:last').find('.c_solutions').html("");
                $('.quoteTable12 tr:last').find('.c_solutions').append(options);
                $(".c_solutions").change();

            });
        });
priya_singh
  • 2,478
  • 1
  • 14
  • 32
CoderJoe
  • 29
  • 9

1 Answers1

1

Since you are adding select dynamically you need to understand delegated events

Description: Attach a handler to one or more events for all elements that match the selector, now or in the future, based on a specific set of root elements.

select you select class like this

$( document ).on( "click", ".js-select2", function() {
  console.log( "hey" );
});
Amar Singh
  • 5,464
  • 2
  • 26
  • 55