0

I know it looks like a basic question. I've been playing around with delegation but still getting the same result.

What I'm trying to make it work the remove button on the list, it only works the first one but if I add a new one it won't work

  //variables
  let $questions = $('.quiz-questions');
  let $tableOptionQuestions = $('.survey-list-service');
  let $addNewOption = $('.add-option');
  let $removeOption = $('.remove-option');
  
  //events
  $addNewOption.on("click", addNewOptionFunc );
  $removeOption.on("click", removeOptionFunc );

  //add new option
  function addNewOptionFunc (e) {
    let $optionRow = $(this).prev().children('tbody').children('tr').first();
    $optionRow.clone().appendTo( "tbody" );
  }
  
  //remove option
  function removeOptionFunc (e) {
    let $optionRow = $(this);
    let $sizeRow = $tableOptionQuestions.find('tbody tr').length; 
    
    // if( $tableOptionQuestions.find('tbody tr').length != 1 ) {
      $optionRow.closest('tr').remove();
    // }    
    
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="quiz-questions">

  
  <section>
  <table class="survey-list-service">
    <thead>
      <tr>
        <th>Name</th>       
        <th>Action</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td><input type="text" value=""></td>
        <td><button class="button button-primary remove-option">remove</button></td>
      </tr>
    </tbody>
  </table>
  
  <button class="button button-primary add-option">Add </button>

  </section>

</div>
marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
Marcogomesr
  • 2,238
  • 3
  • 25
  • 37

1 Answers1

1

Bind events like this for dynamically added elements.

$(document).on("click", '.add-option', addNewOptionFunc);
$(document).on("click", '.remove-option', removeOptionFunc);

//variables
let $questions = $('.quiz-questions');
let $tableOptionQuestions = $('.survey-list-service');

//events
$(document).on("click", '.add-option', addNewOptionFunc);
$(document).on("click", '.remove-option', removeOptionFunc);

//add new option
function addNewOptionFunc(e) {
  let $optionRow = $(this).prev().children('tbody').children('tr').first();
  $optionRow.clone().appendTo("tbody");
}

//remove option
function removeOptionFunc(e) {
  let $optionRow = $(this);
  let $sizeRow = $tableOptionQuestions.find('tbody tr').length;

  // if( $tableOptionQuestions.find('tbody tr').length != 1 ) {
  $optionRow.closest('tr').remove();
  // }    

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="quiz-questions">


  <section>
    <table class="survey-list-service">
      <thead>
        <tr>
          <th>Name</th>
          <th>Action</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td><input type="text" value=""></td>
          <td><button class="button button-primary remove-option">remove</button></td>
        </tr>
      </tbody>
    </table>

    <button class="button button-primary add-option">Add </button>

  </section>

</div>
Nandita Sharma
  • 12,347
  • 2
  • 14
  • 26