append tr in table jquery not working correct

131 Views Asked by At

I want to add new row in table with dynamic values when user click in li element. But when user click it suddenly add 2 rows in table not 1. Why?

This is my jQuery code:

$('.wpforms-field-payment-multiple ul li').click(function(){
    var price = $(this).find('input').data('amount');
    var parent = $(this).closest('.wpforms-field-payment-multiple');
    var titles = $('.wpforms-field-description',parent).html();
    $('#booking .right .price table tbody').append('<tr><td class="name">'+titles+'</td><td class="pr">'+price+'</td></tr>');
});
1

There are 1 best solutions below

1
On

JS looks ok to me but check your HTML. You probably have a nested UL,LI tags.

Example below.

$('.wpforms-field-payment-multiple ul li').click(function() {
  alert("click");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="wpforms-field-payment-multiple">
  <ul>
    <li>
      One Alert
      <ul>
        <li>
          Two Alerts
        </li>
      </ul>
    </li>
  </ul>
</div>