I’m writing some simple jquery, and for some reason, I simply cannot make it work. I /know/ it works elsewhere, but I’m either missing something, or there’s a bug on my webserver.
code follows:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>asdf</title>
<script src="jquery.js" type ="text/javascript"></script>
<script language ="javascript" type = "text/javascript">
//alert("number one");
$('#regSubmit').click(function(){
alert("Clicked!");
});
$('#test2').click(function(){
alert("yeah that worked.");
})
</script>
</head>
<body>
<input type ="button" id="regSubmit" value="Register!" />
<div id="test2">
Here is some other stuff!
</div>
</body>
</html>
So I’ve copied that code exactly, but it doesnt work for me. that commented out alert near the top (number one) works fine, so its calling the jquery, but no clicks register.
What am I doing wrong?
Your code when embedded into HTML is executed sequentially, so at the time of the execution of your code regSubmit and test2 are not yet defined.
Move your code to the bottom of the HTML document, or wrap it in
$(function(){ /*your code here*/ });which will defer your code execution until the entire document is loaded.