<button>Click to Toggle</button>
<p class="noNameSpace">
</p>
<p class="nameSpace">
</p>
JavaScript
//two 'click' events on the button - we'll stop *only* the namespaced 'click' after three runs, so the normal 'click' keeps going
var x=0, count=0;
//this 'click' event has no namespace - we'll let it toggle forever
$('button').on('click', function(){
if (x==1){
$('.noNameSpace').html('our .off unbinding method will only target the namespaced click');
x=0;
} else {
$('.noNameSpace').html('just a normal click event, no namespace');
x=1;
};
});
//this 'click' event has a namespace 'myNamespace', which we'll unbind once the event has run three times (count hits three)
$('button').on('click.myNamespace', function(){
//check if count has reached 3
if (count>=3) {
$('button').off('click.myNamespace');
} else {
//if not yet 3, keep bound and increment 'count' by 1
// not how the "on" is not required when referenceing a namespaced event
// so you can switch all associated events on or off at once
$('.nameSpace').html('you\'ve clicked the namespaced event ' + (++count) + (count==1? ' time' : ' times'));
}
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.