I am trying to understand event binding in jQuery. I have written a simple code as follows:
<html>
<script language="javascript" src="jquery.js"></script>
<body>
<form>
<input type="button" name="bu1" value="click me">
</form>
</body>
<script language="javascript">
jQuery("input[type='button']").bind('click',alert('Hello'));
</script>
</html>
When I run the page, the alert happens on load rather than when I click the button. Can anyone help me to understand where is it I am going wrong?
You need to pass a function, not call it. So in the code above I have added an anonymous function which calls alert.