Link to home
Start Free TrialLog in
Avatar of error77
error77

asked on

Javascript code not working ... assistance needed

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






ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of error77
error77

ASKER

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

Avatar of error77

ASKER

Sorry it got added twice for some reason but they are both the same one.
where do you include the external javascript ?
Avatar of error77

ASKER

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

Thanks :)