This post will discuss how to detect click events on an element using JavaScript and jQuery.

There are several ways to listen to click events with JavaScript and jQuery.

1. Using jQuery

With jQuery, you can use the .on() method to attach event handlers to one or more elements. The following code demonstrates its usage by attaching a click event handler to a submit button.

JS


HTML



Edit in JSFiddle

 
There is a shorthand method in jQuery for the .click() event to attach event handlers.

JS


HTML



Edit in JSFiddle

2. Using JavaScript

With pure JavaScript, you can use the EventTarget.addEventListener() method to bind to click event of an element.

JS


HTML



Edit in JSFiddle

 
Alternatively, you can use the onclick handler for specifying an event listener to receive click events.

JS


HTML



Edit in JSFiddle

That’s all about detecting an element’s click event in JavaScript and jQuery.