jQuery Form How to - Handle click event for button element








Question

We would like to know how to handle click event for button element.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.6.js'></script>
<script type="text/javascript"
  src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.js"></script>
<style type='text/css'>
button.ui-state-default.myClass {<!--   w  ww  .j  a  v a 2s. com-->
  background: #ff0;
}
</style>
</head>
<body>
  <button>Hi</button>
  <script type='text/javascript'>
$('button').button().bind(
    'click',
    function(e) {
        $(this).addClass('myClass');
    }
);

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

The code above is rendered as follows: