jQuery click()

Introduction

Click on a <p> element to alert a text:

View in separate window

<!DOCTYPE html>
<html>
<head>
<script 
 src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>/*  w  w  w  .j  a  v  a 2  s.  c o m*/
<script>
$(document).ready(function(){
  $("p").click(function(){
    document.getElementById("demo").innerHTML = 
             "The paragraph was clicked.";
  });
});
</script>
</head>
<body>

<p id="demo"></p>
<p>Click on this paragraph.</p>

</body>
</html>

The click event occurs when an element is clicked.

The click() method triggers the click event.

The click() method can also run a function when a click event occurs.

Trigger the click event for the selected elements:

$(selector).click()

Attach a function to the click event:

$(selector).click(function)
Parameter Optional Description
function Optional. function to run when the click event occurs



PreviousNext

Related