jeudi 14 novembre 2019

How to reference the element that was clicked in the IF statement

How to hide the p that was clicked in the code? Existing new ones?

(function($) {
  $('body').on('click', function(event) {
    if (event.target == $('button')[0]) {
      $('body').append('<p class="myp">text</p>')
    }

    if ($(event.target).attr('class') == 'myp') {
      // hide the clicked p
    }
  })
})(jQuery)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button>Add</button>
<p class="myp">text</p>
<p class="myp">text</p>

Aucun commentaire:

Enregistrer un commentaire