I have a jQuery code that works when I insert it inside a script tag within the body. But it doesn't work when I insert it in the head tag or include it as a js file.
+3
A:
Judging from your description you most probably forgot to put your script into a $(document).ready
function:
$(function() {
// put your code here
});
Then you can place this wherever you want on the page (head
, body
, external script, ...).
Darin Dimitrov
2010-07-15 07:01:14
Thanks..! It Worked...
ahmedmzl
2010-07-15 07:21:00
This also worked while including it as a js file
ahmedmzl
2010-07-15 07:21:53
@Darin Dimitrov: Great mind reading abilities! =)
Tommy
2010-07-15 21:36:22
A:
Any script code put inside a head is not supposed to execute automatically. Unless called by a event listener. So if you have put your code in the head section its just a piece of functionality waiting to be executed on a call. This code can be called as mentioned on a document.ready( ) event handler or any other event call from the user.
sushil bharwani
2010-07-15 07:05:38
A:
try these format
<html>
<head>
<script>
$(document).ready(function() {
$('#arrow img').click(function() {
transferEmp('test');
});
});
</script>
<body>
<!-- other tags -->
<script>
$(document).ready(function() {
$('#arrow img').click(function() {
transferEmp('test');
});
});
</script>
<script src="test.js" type="text/javascript"></script>
</body>
</html>
Jaison Justus
2010-07-15 07:07:08