[Javascript] onclick - unobtrusive javascript

J. Lester Novros II lester at denhaag.org
Sat Jul 23 04:48:16 CDT 2005


Tim Burgan wrote:
> When an element is clicked, I am unable to change it's text color to red.
> But for the same element, the code does work if I choose to open a new 
 > window on click.

The 'i' within the anonymous function assigned to each link's onclick handler is 
never resolved to a number since by the time you actually click on the link, the 
program has exited the for loop. Try using 'this.style.color' instead [see below].

   function doPopups()
   {
      if ( !document.getElementsByTagName )
         return false;

      var links = document.getElementsByTagName("a");
      for ( var i = 0; i < links.length; i++ )
      {
         if ( links[i].className.match("change") )
         {
            links[i].onclick =
            function()
            {
               this.style.color = 'red';
               return false;
            }
         }
      }
   }

l8R lES
-- 
"Anti-virus products for Unix servers occupy a useful niche in the market, not
because there are many viruses that infect Unix platforms but because they help
prevent these servers from hosting Windows malware."  John Leyden - the Register
                                                           http://www.denhaag.org



More information about the Javascript mailing list