- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
jQuery.click() vs onClick
In this article, we will learn the difference between the jQuery.click() and onClick. Let us first understand what is click() and onClick() in jQuery.
jQuery click()
Example
The click() method triggers the click event of each matched element. Let us see an example −
<html> <head> <title>The jQuery click() Example</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script> <script language = "javascript"> $(document).ready(function() { $("#clickButton1").click(function(){ alert("click event occurred"); }); }); function clickFirstButton(){ $("#clickButton1").click(); } </script> </head> <body> <form name = "sampleForm"> <label>Enter Name: </label><input type = "text" id = "result1" title="Enter Name" value = "" ></input><br/><br/> <input type = "button" id = "clickButton1" value = "Click Me!" /> <input type = "button" id = "clickButton2" value = "Click First Button!" onclick="clickFirstButton();" /> </form> </body> </html>
Output

Click the Click Me button &mius;

Click the Click First Button button −

onClick
The onclick() event type occurs when a user clicks the left button of his mouse. You can put your validation, warning etc., against this event type.
Example
Let us see an example −
<!DOCTYPE html> <html> <head> <script> function myAttrFunc() { var a = document.getElementsByTagName("a")[0].getAttribute("href"); document.getElementById("Demo").innerHTML = a; } </script> </head> <body> <h1>Courses</h1> <a href="https://www.tutorialspoint.com/market/index.asp">Tutorialspoint Courses</a> <p>Clicking the below button to get the above link:</p> <button onclick="myAttrFunc()">GET THE LINK</button> <p id="Demo"></p> </body> </html>
Output

Click GET THE LINK button to display the link −

Using click() is better as it follows standard event registration model. You can attach multiple handlers to the same element. You can easily access the event object, and the handler can live in any function's scope. Also, it is dynamic, i.e. it can be invoked at any time and is especially well-suited for dynamically generated elements. Whether you use jQuery, another library or the native methods directly does not really matter.
- Related Articles
- JavaScript function in href vs. onClick
- HTML onclick Event Attribute
- What is onclick event in JavaScript?
- Call a function with onclick() – JavaScript?
- How to call multiple JavaScript functions in onclick event?
- Why is using onClick() in HTML a bad practice?
- How can I get onclick event on webview in Android?
- How to call a JavaScript function from an onClick event?
- How to check if onClick exists on element in jQuery?
- OneDrive vs Dropbox vs Google Drive vs Box
- Virtual vs Sealed vs New vs Abstract in C#
- How to Set OnClick Listener on Action Bar Title in Android?
- C++ vs Java vs Python?
- Corona vs. Phonegap vs. Titanium
- mysql_fetch_array vs mysql_fetch_assoc vs mysql_fetch_object?
