Languages
[Edit]
EN

jQuery - mouse click event

9 points
Created by:
maryam
1181

In this article, we would like to show you how to create a click event using jQuery. The function works similarly to the onclick event.

1. click method example

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
</head>
<body>
  <button id="button">Click me!</button>
  <script>

    $(document).ready(function() {
		var button = $('#button');
      
      	button.click(function() {
        	button.text('Button clicked!');
        });
    });

  </script>
</body>
</html>

2. on method with event name example

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
</head>
<body>
  <button id="button">Click me!</button>
  <script>

    $(document).ready(function() {
		var button = $('#button');
      
      	button.on('click', function() {
        	button.text('Button clicked!');
        });
    });

  </script>
</body>
</html>

3. on method with event object example

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
</head>
<body>
  <button id="button">Click me!</button>
  <script>

    $(document).ready(function() {
		var button = $('#button');
      
      	button.on({
            'click': function() {
        	    button.text('Button clicked!');
            }
        });
    });

  </script>
</body>
</html>
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

jQuery

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join