Solved

Jquery not executing

Posted on 2010-08-23
4
334 Views
Last Modified: 2012-05-10
the code seems to be working when i do;
$(document.body).click(function ()

but not with a specific id


<!DOCTYPE>

<html>

	<head>

		<title>TEST</title>

		<link href="style.css" media="screen" rel="stylesheet" type="text/css" />

                     <script src="http://code.jquery.com/jquery-latest.min.js"></script>

	</head>

	<body>

	<script>



	$('#location').click(function ()

	{

	if ($("#aa:first").is(":hidden"))

	{

	$("#aa").slideDown("slow");

	}

	else

	{$("#aa").hide();

	}

});</script>









 <div id="location" class="location"><a href="#"> <div id="aa"></div></div>







	</body>



</html>

Open in new window

0
Comment
Question by:fb19071907
  • 2
4 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 33503701
Check this :


<!DOCTYPE>
<html>
<head>
<title>TEST</title>
<link href="style.css" media="screen" rel="stylesheet" type="text/css" />
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
	$(document).ready(function() {
		$('#location').click(function () {
			if ($("#aa:first").is(":hidden")) {
				$("#aa").slideDown("slow");
			}
			else {
				$("#aa").hide();
			}
		});
	});
</script>
</head>
<body>
<div id="location" class="location"><a href="#">Click</a><div id="aa">HELLO</div></div>
</body>
</html>

Open in new window

0
 
LVL 6

Expert Comment

by:CyberGhost
ID: 33504776
I believe you're missing a closing </a> tag in your code. Other than that, I would relocate your script into <head> part of code, just as asleakim971 pointed out.
0
 
LVL 15

Expert Comment

by:SRigney
ID: 33504947
inline javascript like that will work, but it must be after the item that it's trying to reference.

If you put it into the document ready function that leakim showed you, then the html will be finished loading before the javascript runs making the order not important.


CyberGhost, the recommendations for speed is CSS at the top of the page and javascript at the bottom of the page.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33514329
Thanks for the points!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now