javascript - Jquery Event Handler ".on()" -


i have problem jquery event handler ".on()".

i have button favorite on post on home page.

if haven't favorited post:

  • li not active
  • event 1

if have favorited post:

  • li active
  • event 2

why when click multiple times (>1) on button, script same method (event) whereas have class .active on li or not?

it's .on() doesn't manage dynamic class change...

my html:

<ul class="post-list">     <li>         <a href="#" class="favorite">favorite</a>     </li>     <li class="active">         <a href="#" class="favorite">favorite</a>     </li> </ul> 

my jquery:

$(function(){     $('.post-list li.active .favorite').on('click', function(e){         e.preventdefault;         // event 2     });     $('.post-list li:not(.active) .favorite').bind('click', function(e){         e.preventdefault;         // event 1     }); }); 

jquery binds events result of selectors (once). hoping selector re-computed each time event fires, @ point handlers have been bound whichever elements matched selector originally.

try:

$(function(){     $('.post-list li .favorite').on('click', function(e){         e.preventdefault();         if (e.target.hasclass('active') {            //event 2         }         else {            //event 1         }     }); }); 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -