views:

50

answers:

3

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
Thanks..! It Worked...
ahmedmzl
This also worked while including it as a js file
ahmedmzl
@Darin Dimitrov: Great mind reading abilities! =)
Tommy
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
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