Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problem with globals variables

    Hi,
    First of all, Let me explain what I want to do:

    I built a helpdest for the support in our entreprise and I've been asked to do it so when they click on a link it Hightlight it. Highlghting the link was easy, but the the previously hightlighted object were still highlighted. So I figured that if I could copy the link object in a global variable I could modify it afterwar. howeve it's not working for some reason

    There's the fuction im using:

    Code:
    	<script language="JavaScript">
    	<!--
    	var previousLink;
    	function selectLink(lnk) {
    	  	lnk.style.fontWeight='bolder';
    	  	lnk.style.textDecoration='underline';
    	  	previousLink.style.textDecoration='none';
    		previousLink = lnk;
    	}
    	//-->
    	</script>
    And there's the links im Generate with PHP
    Code:
    <a target="bugdetails" href="/foo/bar.php?id=6969" onclick="selectLink(this)">fubar help</a>
    I was thinking of passing lnk by referennce but it seem it's doesnt work.

    I hope I have ennough details and that my question isn't too noobish.

    Thank a lot

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Posts
    400
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The problem is that the first time you click a link, previousLink is undefined. Therefore previousLink.style.textDecoration='none'; results in an error and the function terminates. And since the function terminates before the line previousLink = lnk, previousLink will never get a valid assignment.

    The solution is simple, test previousLink.
    Code:
    <script type='text/javascript'>
    var previousLink;
    function selectLink(lnk) {
      lnk.style.fontWeight='bolder';
      lnk.style.textDecoration='underline';
      if(previousLink){
        previousLink.style.textDecoration='none';
        previousLink.style.fontWeight='normal';
        }
      previousLink = lnk;
      }
    </script>

  • #3
    New to the CF scene
    Join Date
    May 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Wow it work like wonder, I've been looking around for soo long to find what was wrong. For some reason the Javascript console wouldnt report anyting.

    Thank a lot the support guys will be very happy


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •