Javascript code not working ... assistance needed

error77
error77 used Ask the Experts™
on
Hi all,

I am having an issue with some code as it's not working for some reason. I wonder if anyone could spot what the problem is.

I'll explain:

In an external JS file I have this code:

$("a").click(function() { if( $(this).attr("id") == "link2" ) {
                alert("id was clicked");
                  }});

Then I have some javascript for the link in the head that takes care of the link:

<script language="javascript">
      $(document).ready(function() {
      
         var thisyear = new Date().getFullYear();
       var strLink = "index2.php?mydate=" + thisyear + "-1-1";
       document.getElementById("link2").setAttribute("href",strLink);

.......


And finally the link:

<a id="link2" rel="external"> Link Here</a>


The links DO work ... the problem is that the code on the JS file is not working...

When I click on the link it should show the alert but it's not.

Can anyone help please?

Thanks






Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Multitechnician
Top Expert 2014
Commented:
Replace :
$("a").click(function() { if( $(this).attr("id") == "link2" ) {
                alert("id was clicked");
                  }});

Open in new window

By:

$("a").live("click", function() { if( $(this).attr("id") == "link2" ) {
                alert("id was clicked");
                  }});

Open in new window

Author

Commented:
OK here is the full code of the html page.

It's supposed to show the alert message but nothing happens..

Here is the code
<!DOCTYPE html>
<html>
<head>
	
	<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.min.js"></script>

  


<script language="javascript">
	$(document).ready(function() {
	
	   var thisyear = new Date().getFullYear();
       var strLink = "index2.php?mydate=" + thisyear + "-1-1";
       document.getElementById("link2").setAttribute("href",strLink);
	});
	
</script>

<style>
	a {
		text-decoration:none;
		background-color:transparent;
		color:#fff;
	}
</style>
	
	
</head> 
<body> 
<a id="link2" rel="external"> link here </a>

</body>
</html>

Open in new window

<!DOCTYPE html>
<html>
<head>
	
	<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.min.js"></script>

  


<script language="javascript">
	$(document).ready(function() {
	
	   var thisyear = new Date().getFullYear();
       var strLink = "index2.php?mydate=" + thisyear + "-1-1";
       document.getElementById("link2").setAttribute("href",strLink);
	});
	
</script>

<style>
	a {
		text-decoration:none;
		background-color:transparent;
		color:#fff;
	}
</style>
	
	
</head> 
<body> 
<a id="link2" rel="external"> link here </a>

</body>
</html>

Open in new window

Author

Commented:
Sorry it got added twice for some reason but they are both the same one.
leakim971Multitechnician
Top Expert 2014

Commented:
where do you include the external javascript ?

Author

Commented:
It worked after changing the position where the js file was included.

Thanks :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial