Home > Blockchain >  Adding .not() on event handling to dynamically added content
Adding .not() on event handling to dynamically added content

Time:03-06

With event handling on dynamically added content, I use:

$('#static-div').on('click', '.dynamic-content', function () {
    // do something
});

What should I do if I want to add a .not() to this code? As in, I want to click anything with ".dynamic-content" but not apply the event handling to ".should-not-be-clicked"

CodePudding user response:

Add :not() to the selector

$('#static-div').on('click', '.dynamic-content:not(.no)', function () {
    console.log('clicked')
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="static-div">
  <button type="button" >Y</button>
  <button type="button" >N</button>
  <button type="button" >Y</button>
  <button type="button" >N</button>
</div>

CodePudding user response:

There is a not css selector which you can add to this.

Check it out here

$('#static-div').on('click', '.dynamic-content:not(.should-not-be-clicked) ', function () {
    // do something
});
  • Related