Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add click event after another click event

I am trying to add a click event to the document in another click event attached to a button. However, the second click event is fired right away as if the event overlaps. I looked into stopping propagation, using a timeout, removing the listener, preventDefault(), but I've had no success.

This is an example of what I am trying to do.

document.getElementById("test").addEventListener('click', first);

function first(){
    document.addEventListener('click', second);
}
function second(){
    alert("I'm not suppose to appear after the first click, only the second.");
}

For testing, I am using a simple button

<button type="button" id="test">Click</button>

I am doing this without JQuery. Is this possible?

like image 251
Alexander Weihmayer Avatar asked Oct 21 '15 14:10

Alexander Weihmayer


People also ask

How do you trigger an event on click?

If you just need to trigger a click event, you can omit the line that begins with for( . @Parag: Read it again. The loop is to click the same link 50 times, which is what it does.

Can you add multiple click event listeners?

The addEventListener() methodYou can add many event handlers of the same type to one element, i.e two "click" events. You can add event listeners to any DOM object not only HTML elements.

How do I create a one time event listener?

Use modern JavaScript! EventTarget. addEventListener("click", function() { // Do something cool }, {once : true}); A Boolean indicating that the listener should be invoked at most once after being added.

Why does addEventListener only work once?

We can pass an object as an argument to the addEventListener method and specify that the event is only handled once. This is achieved by passing the property once to the object. If we set once to true, the event will only be fired once.

How to trigger click event on any element in Dom?

Most of the elements in the DOM support click () method. We can leverage this method to trigger click event on any element. When you call click () method on an element, a click event is dispatched and event listener in response will execute event handler function. We are going to cover the same technique.

How to attach an event handler on click event of button?

As you click on the button, the .on method will attach an event handler on click event of the button. Have a look at live demo of this example along with code: As you can see, the jQuery on method specifies click event along with calling a function animateex.

How do I assign the onclick event to the window object?

Assign the "onclick" event to the window object: window.onclick = myFunction; // If the user clicks in the window, set the background color of <body> to yellow. function myFunction () {.

How to use jQuery on method for click event?

The $.on method is linked with the button’s click event. As you click on the button, the .on method will attach an event handler on click event of the button. Have a look at live demo of this example along with code: As you can see, the jQuery on method specifies click event along with calling a function animateex.


1 Answers

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>
like image 145
guest271314 Avatar answered Sep 25 '22 11:09

guest271314