I am learning jquery and I am having difficulty running the following extremely simple code.
My HTML looks like:
<html>
<head>
<title>JQUERY</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<input type="button" id="testbutton" value="test" />
</body>
My script.js looks like this:
$(document).ready(function(){
alert('document is ready');
});
$('#testbutton').click(function(){
alert('button clicked');
});
When I load the page in browser … I get the first alert when the document is loaded (so I am sure that I am at least using the correct library and it is getting downloaded etc).
However, when I click my button I do not see the second alert that says “button has been clicked”
What am I missing?
The
clickhandler needs to go inside your$(document).ready, to ensure that it is bound when the DOM is ready, and the element is guaranteed to be available for Javascript processing:From the documentation: