What is jQuery’s Event.namespace for

Asked

Viewed 60 times

1

In the jQuery documentation, there is the explanation of event.namespace.

This allows you to use an event (or create an event) specific to jQuery, with a namespace, through a . point and a name in front of that event.

Example:

$(document).on('click.first_event', function (event)
{
     alert(event.namespace);
});

I would like to know what is the advantage of using events with these namespaces in jQuery?

What are these for namespaces?

  • 1

    Note: Not a duplicate of http://answall.com/questions/47956/eventos-com-namespace

  • I don’t think it’s a duplicate, it asks how to replicate in pure javascript.

  • But the staff was very quick to mark as duplicate. I also do not agree - or else I would not have asked.

  • Wallace is not really a message for you, but for the analysis queue, when someone goes to click to close, will read my comment next to the question, so I put my opinion that this is not a duplicate, I commented through the analysis queue directly and marked to leave open.

1 answer

0

A event.namespace can be useful for controlling which specific events I want to remove.

Example:

Suppose I have three events of scroll linked to window.

I want to remove one of them specifically, but not all of them.

If I did so:

$(window).scroll(function (){  
      alert('1');
   }

   $(window).scroll(function (){  
      alert('1');
   }

   $(window).off('scroll');

I would remove all events.

But if I did so, I would succeed, thanks to event.namespace.

$(window).bind('scroll.alert_1', function ()
{
     alert('1');
});

$(window).bind('scroll.alert_1', function ()
{
     alert('2');
});

$(window).unbind('scroll.alert_1');

In the above case, only the alert('2'), since I was able to remove a specific event on account of that namespace.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.