JQuery namespace function

What is the difference between these two pieces of code? Both work fine, so why use .dropdown.data-api in a function? I read about the namespace on the Internet, but I do not quite understand about it. Can someone tell me what the use of namespace functions is?

$('html').on('click.dropdown.data-api',
  function () {
    $el.parent().removeClass('open')
  })
}

$('html').on('click',
  function () {
    $el.parent().removeClass('open')
  })
}
+5
source share
1 answer

Namespace placement allows you to target a specific event if you want to, say, untie it or trigger it.

Imagine that you have two events of the same type associated with the same element.

$('.something').on('click', function() { /* do something */ });
$('.something').on('click', function() { /* do something else */ });

Since we did not use the namespace in any of the events, it is now difficult to untie or call one and not the other. Now consider:

$('.something').on('click.one', function() { /* do something */ });
$('.something').on('click.two', function() { /* do something else */ });

, , .

$('.something').off('click.one'); //unbind the 'one' click event
$('.something').trigger('click.two'); //simulate the 'two' click event

[ - @jfrej, , . , , off('.namespace').]

+6

All Articles