Skip to content

Instantly share code, notes, and snippets.

@scabbiaza
Created September 1, 2015 09:20
Show Gist options
  • Select an option

  • Save scabbiaza/ecab1f18f5c168d3e9b5 to your computer and use it in GitHub Desktop.

Select an option

Save scabbiaza/ecab1f18f5c168d3e9b5 to your computer and use it in GitHub Desktop.
Bind/unbind with namespaces
// handler for click event
var h1 = $("h6").bind("click", function () {
console.log('click handler');
});
// handler for click event with namespace
var h2 = $("h6").bind("click.namespace", function () {
console.log('click handler with namespace');
});
// click event result:
// > click handler
// > click handler with namespace
// next click event result:
// > click handler
// > click handler with namespace
//------------------------------------------------------------------------------
// handler for click event
var h1 = $("h6").bind("click", function () {
console.log('click handler');
});
// handler for click event with namespace and unbind by this namespace
var h2 = $("h6").bind("click.namespace", function () {
console.log('click handler with namespace');
$("h6").unbind("click.namespace");
});
// click event result:
// > click handler
// > click handler with namespace
// next click event result:
// > click handler
//------------------------------------------------------------------------------
// handler for click event and unbind click event
var h1 = $("h6").bind("click", function () {
console.log('click handler');
$("h6").unbind("click");
});
// handler for click event with namespace
var h2 = $("h6").bind("click.namespace", function () {
console.log('click handler with namespace');
});
// click event result:
// > click handler
// > click handler with namespace
// next click event result:
// > [nothing here]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment