Categories
JavaScript Answers

How to wait for a click event to complete with JavaScript?

Spread the love

Sometimes, we want to wait for a click event to complete with JavaScript.

In this article, we’ll look at how to wait for a click event to complete with JavaScript.

How to wait for a click event to complete with JavaScript?

To wait for a click event to complete with JavaScript, we can add a click listener function.

For instance, we write:

<div>
  hello world
</div>

to add a div.

Then we write:

const div = document.querySelector('div');
div.onclick = () => {
  console.log('clicked')
}

to select the div with querySelector.

Next, we set div.onclick to a function that logs 'clicked'.

Now when the click event is emitted, we see 'clicked' logged.

Conclusion

To wait for a click event to complete with JavaScript, we can add a click listener function.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *