BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

jQuery click() Method

Last Updated: April 27, 2019 by Chaitanya Singh | Filed Under: jQuery

jQuery click event occurs when you click on an html element. jQuery click() method is used to trigger the click event. For example $(“p”).click() will trigger the click event when a paragraph is clicked on a document (a web page). We can attach a function to the click() method to run the function when a click event occurs, this way we can run a piece of code every time the click event is triggered. In this guide, we will learn about jQuery click() method.

jQuery click() Method Syntax

$(selector).click(function(){
  //block of code that runs when the click event triggers
});

jQuery click() event example

In the following example, when you click on the paragraph, it gets hidden. This happens because we have associated the click() method to the paragraph selector and in the custom function we are hiding the current element using hide() method. The custom function inside click event runs when a click on a paragraph happens.

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("p").click(function(){
    $(this).hide();
  });
});
</script>
</head>
<body>

<h2>jQuery click event example</h2>
<p>This example is published on beginnersbook.com. This paragraph will be
hidden when you click on it.</p>
</body>
</html>

Output:
Before clicking on the paragraph:
jQuery click() method
After clicking on the paragraph:
jQuery click event

Another example of jQuery click event

In the following example, we have associated the click method to the id selector, we have provided the button id, so when we click on the button, the click event is triggered. In the click event function we are hiding the h2 elements using hide() method.

You can see in the output screenshots that when you click on the “Hide Heading” button, the heading of the document gets hidden.

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("#myid").click(function(){
    $("h2").hide();
  });
});
</script>
</head>
<body>

<h2>jQuery click event example</h2>
<p>This example is published on beginnersbook.com. The main heading
will be hidden when you click on the "Hide Heading" button</p>
<button id="myid">Hide Heading</button>
</body>
</html>

Output:
Before clicking the button:
jQuery click event example
After clicking the button:
jQuery click event triggered on the button click

❮ PreviousNext ❯

Top Related Articles:

  1. jQuery html() method
  2. jQuery Sliding Effects
  3. jQuery Multiple Elements Selector
  4. jQuery removeClass() Method
  5. jQuery slideToggle()

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

jQuery Tutorial

  • jQuery Tutorial
  • jQuery Selectors
  • jQuery Events

jQuery Effects

  • jQuery Show & Hide
  • jQuery Fading
  • jQuery Sliding
  • jQuery Animate
  • jQuery stop()
  • jQuery Callback Function
  • jQuery Chaining

jQuery HTML/CSS

  • jQuery html()
  • jQuery addClass()
  • jQuery hasClass()
  • jQuery removeClass()
  • jQuery toggleClass()
  • jQuery after()
  • jQuery before()
  • jQuery append()
  • jQuery appendTo()
  • jQuery clone()
  • jQuery insertBefore()
  • jQuery insertAfter()
  • jQuery attr()
  • jQuery text()
  • jQuery val()
  • jQuery css()
  • jQuery prepend()
  • jQuery remove()
  • jQuery empty()
  • jQuery detach()

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap