jQuery event namespace


/ Published in: jQuery
Save to your folder(s)

Adding a namespace to an event can make it easier to unbind. It is very easy to unbind all events with the same namespace.


Copy this code and paste it in your HTML
  1. //Bind Event One
  2. $("a").bind("click.nameOne", function(){
  3. console.log("Event One Fire!");
  4. return false;
  5. });
  6.  
  7. //Bind Event Two
  8. $("a").bind("click.nameTwo", function(){
  9. console.log("Event Two Fire!");
  10. return false;
  11. });
  12.  
  13. //Unbind all nameTwo events
  14. $("a.utwo").click(function(){
  15. $("a").unbind(".nameTwo");
  16. });
  17. //Unbind all nameOne events
  18. $("a.uone").click(function(){
  19. $("a").unbind(".nameOne");
  20. });

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.