Code Explanation: HTML: You have a simple button with the ID myButton. jQuery: The script waits until the document is fully loaded ($(document).ready()). Once it’s ready, the click event is bound to the #myButton jQuery on click button element. Function: When the jQuery on click button is clicked, an alert box appears with the message \"Button clicked!\". Output: jQuery on click event Example 2: Code Explanation: HTML: Two buttons are provided: #triggerButton to simulate the click, and #myButton to display the alert. jQuery: When the jQuery on click button #triggerButton is clicked, it will programmatically trigger the click event of #myButton. This simulates a click without the user needing to click the button themselves. Output: Also Read: Top 25+ JavaScript Frameworks to Learn in 2025 & How to Choose the Right One Now that you understand what the jQuery .click() function does, let's dive into how it’s implemented in real-world code. Implementation of the .click() Method in jQuery In jQuery, the .click() method can be implemented in two main ways: By passing a function as an argument that runs on click By triggering the click programmatically without any argument Let’s go through both approaches step-by-step along with practical examples, output explanations, and how they behave differently. 1. Using .click() With a Function Argument When you pass a function to .click(), it gets executed every time the selected element is clicked. This is the most common way to make web elements interactive using jQuery. jQuery on click event Example Code:
This is some text.
Code Explanation: You have anWaiting for click...
Code Explanation: Clicking on thewith ID target. Since the target has its own .click() function defined (which shows an alert), this gets executed indirectly. Output: 3. Attaching .click() to a Button to Update Content A very common use of .click() is attaching it to a jQuery on click button to dynamically update the page content, such as inserting or replacing text. jQuery on click event Example Code: