mardi 9 février 2016

jQuery doesn't allow dynamic event listeners

 var action = 'click';
 if (action == 'click') {
      $('#element').on(action, function() {
           // do something...
      })
 } else if (action == 'mouseenter') {
      $('#element').on(action, function() {
           // do something completely different...
      })
 }

Whenever action changes, the event goes off according to the old action; It doesn't change dynamically. I also tried:

 $('#element').on(action, function() {
      if (action == 'click') {
           // do something...
      } else if (action == 'mouseenter') {
           // do something else...
      }
 });

For smart snowflakes: I need one element to be treated differenly depending on what action is, not simultaneous. And the action itself is changeable by the client.




Aucun commentaire:

Enregistrer un commentaire