5 Replies - 29069 Views - Last Post: 12 June 2009 - 09:23 AM

#1 coryroc   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 18-February 09

Running jQuery inside <body> tag?

Post icon  Posted 12 June 2009 - 08:52 AM

Ok, so I'm not sure if this can be done, but I'm really hoping it can be. I have jQuery.js referenced in the <head> of my document. Now, what i'm trying to do is run the following script:

<body>
...
<script type="text/javascript">
$("a#tryIt").click(function() {
   alert("Foo");
});
</script>
...
<a href="http://www.domain.com" id="tryIt">My Link</a>
</body>



However, this is not working. Is it because it's in the <body>? If so, is there anyway to make it work within the body? Any help is greatly appreciated! I'm only 1 day into to learning jQuery.

This post has been edited by coryroc: 12 June 2009 - 08:53 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Running jQuery inside <body> tag?

#2 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Running jQuery inside <body> tag?

Posted 12 June 2009 - 08:53 AM

Moved to JQuery :)
Was This Post Helpful? 0
  • +
  • -

#3 girasquid   User is offline

  • Barbarbar
  • member icon

Reputation: 108
  • View blog
  • Posts: 1,825
  • Joined: 03-October 06

Re: Running jQuery inside <body> tag?

Posted 12 June 2009 - 09:12 AM

It's definitely not breaking because you have your jQuery reference within the <head> of your document - but there isn't enough in your example to show why it's breaking. By all rights, that code should be running.
Was This Post Helpful? 0
  • +
  • -

#4 coryroc   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 18-February 09

Re: Running jQuery inside <body> tag?

Posted 12 June 2009 - 09:21 AM

when i put that code in the following external js file (which is referenced in the <head> section, it works just fine. The contents of that file are:

$(document).ready(function() {													 
	$("#searchbox").focus(
		function() {
			$(this).addClass('black').removeClass('gray').attr('value','');
		});
	
	$("#searchbox").blur(function() {
		$(this).addClass('gray').removeClass('black').attr('value','enter search term');
	});
});




EDIT: i figured it out...I moved the the script to the bottom and now it's working. D'oh.

<body>
<a href="http://www.domain.com" id="tryIt">My Link</a>
...
<script type="text/javascript">
$("a#tryIt").click(function() {
   alert("Foo");
});
</script>
</body>


This post has been edited by coryroc: 12 June 2009 - 09:22 AM

Was This Post Helpful? 0
  • +
  • -

#5 RudiVisser   User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,566
  • Joined: 05-June 09

Re: Running jQuery inside <body> tag?

Posted 12 June 2009 - 09:21 AM

It is because the link is after your Javascript. Move the link above it/javascript below the link and it should work fine. It's trying to select an element that doesn't yet exist.

EDIT: Aah you fixed it, spot on :)

This post has been edited by MageUK: 12 June 2009 - 09:23 AM

Was This Post Helpful? 0
  • +
  • -

#6 coryroc   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 18-February 09

Re: Running jQuery inside <body> tag?

Posted 12 June 2009 - 09:23 AM

yep...just figured that out as you posted. edited my last post.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1