Add click event after another click event
Try using event.stopImmediatePropagation()
document.getElementById("test").addEventListener('click', first);
function first(e){
e.stopImmediatePropagation();
this.removeEventListener("click", first);
document.onclick = second;
}
function second(){
alert("I'm not suppose to appear after the first click, only the second.");
}
<button type="button" id="test">Click</button>
You can use a variable that keeps count of the clicks done
document.getElementById("test").addEventListener('click', clickHandler);
var clickCount=0;
function clickHandler(event){
clickCount++;
if(clickCount==2){
event.target.removeEventListener("click");
document.addEventListener('click', function(){
alert("I'm not suppose to appear after the first click, only the second.");
});
}
}
If you don't want to use a global variable you can use dataset, make a button with this:
<button type="button" id="test" data-clickcount="0">Click</button>
And use this code:
document.getElementById("test").addEventListener('click', clickHandler);
function clickHandler(event){
event.target.dataset.clickcount++;
if(event.target.dataset.clickcount==2){
event.target.removeEventListener("click");
document.addEventListener('click', function(){
alert("I'm not suppose to appear after the first click, only the second.");
});
}
}