1
votes

I would like to dynamically add a row in my HTML table on click of an add button. The row contains a select element which uses the selectpicker class. When I remove the selectpicker class in my script to add a new row, the select element perfectly appears in the newly created row. But when I use the selectpicker in the script, the element does not render.

This is my table:

$(".addRow").click(function() {
      $(this).closest('table').find('tr:last').prev().after('<tr><td> <
        select name = "myselect[]"
        id = "myselect"
        class = "selectpicker" >
        <
        option > --Select Resp.Center-- < /option> <
        option value = "1" > My Option 1 < /option> <
        option value = "2" > My Option 2 < /option> <
        option value = "3" > My Option 3 < /option> <
        /select> <
        /td> <
        td >
        <
        a href = "#"
        class = "removeRow" > < i class = "fa fa-minus-square fa-fw" > < /i></a >
        <
        /td> <
        /tr>');
      });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="myTable">
  <tr class="myRow">
    <td>
      <select name="myselect[]" id="myselect" class="selectpicker" data-live-search="true">
        <option>--Select Option--</option>
        <option value="1">My Option 1</option>
        <option value="2">My Option 2</option>
        <option value="3">My Option 3</option>
      </select>
    </td>
    <td><a href="#" class="addRow"><i class="fa fa-plus-square"></i></a></td>
  </tr>
</table>
3
Solved this already. Just made the bootstrap-select.js load last. - Chane

3 Answers

0
votes

Strings can't have linebreaks in them. Just remove the linebreaks and it renders fine.

$(".addRow").click(function (elem) {
    $(this).closest('table').find("tr:last").after('<tr><td><select name="myselect[]" id="myselect" class="selectpicker"><option>--Select Resp. Center--</option><option value="1">My Option 1</option><option value="2">My Option 2</option><option value="3">My Option 3</option></select></td><td><a href="#" class="removeRow"><i class="fa fa-minus-square fa-fw"></i></a></td></tr>');
});
0
votes

You need to change your selector, also you need to concatenate the select options.

$(".addRow").click(function() {
  $(this).closest('table').find('tr:last').after('<tr><td> ' +
    '<select name="myselect[]" id="myselect" class="selectpicker">' +
    '   <option>--Select Resp. Center--</option>' +
    '  <option value="1">My Option 1</option>' +
    ' <option value="2">My Option 2</option>' +
    '<option value="3">My Option 3</option>' +
    '</select>' +
    '</td>' +
    '<td>' +
    '   <a href="#" class="removeRow"><i class="fa fa-minus-square fa-fw"></i></a>' +
    '</td>' +
    ' </tr>');
});
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="myTable">
  <tr class="myRow">
    <td>
      <select name="myselect[]" id="myselect" class="selectpicker" data-live-search="true">
        <option>--Select Option--</option>
        <option value="1">My Option 1</option>
        <option value="2">My Option 2</option>
        <option value="3">My Option 3</option>
      </select>
    </td>
    <td><a href="#" class="addRow"><i class="fa fa-plus-square"></i></a></td>
  </tr>
</table>
0
votes

Solved this by placing the bootstrap-select.js (for selectpicker) after all js files in my php file to make it load last.