test.html:
<html>
<head>
<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript" src="test.js"></script>
</head>
<body>
<input id="but2" type="button" value="2"/>
</body>
</html>
jquery-1.4.2.js is downloaded from http://jquery.com/
test.js:
var fn=function(){
alert('success!');
};
$('#but2').click(fn);
When clicked the button, nothing happened. I debugged for very long time but didn't find the root cause. Please help.
but2as the DOM is not loaded yet. Either move the script to the button of the page or, and this is the preferred solution, do as Patrick suggests.