Skip to content
Related Articles

Related Articles

How to run a code on click event in jQuery ?

Improve Article
Save Article
  • Last Updated : 31 Mar, 2021
Improve Article
Save Article

In this article, we will see how to run the code after clicking on an event. To run the code after clicking an event, we use the click() method. The click() method is used to run the code when a click event occurs.

Syntax:

$(selector).click(function);

Parameter: It accepts an optional parameter “function” which is used to run when a click event occurs.

Example:

HTML




<!DOCTYpe html>
<html>
  
<head>
    <title>
        How to run a code on
        click event in jQuery?
    </title>
  
    <script src=
    </script>
  
    <!-- jQuery code to show the working 
        of this method -->
    <script>
        $(document).ready(function () {
               $("body").css("text-align", "center");
               $("h1").css("color", "green");
               $("p").click(function () {
               $(this).css("font-size", "20px");
               $(this).css("color", "green");
            });
        });
    </script>
</head>
  
<body>
    <h1>GeeksforGeeks</h1>
  
    <h3>
        How to run a code on click event in jQuery?
    </h3>
  
    <p onclick="alert('paragraph was clicked')">
        GeeksforGeeks: A computer science portal
    </p>
  
</body>
  
</html>


Output:

click event 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!