Links

Lists

Latest Updates

Ruby On Rails List
Python list
Advanced Java
The JavaScript List
Apache Users
Full Disclosure
Linux Security

Search the archives!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Javascript] onclick - unobtrusive javascript


  • From: email at timburgan.com (Tim Burgan)
  • Subject: [Javascript] onclick - unobtrusive javascript
  • Date: Sat Jul 23 01:14:07 2005

Hello,

I'm trying to use some unobtrusive Javascript by separating behavior 
from structure.

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.

Is there any particular reason why I can't get this to work? My code is 
attached below.

Thanks

Tim



<script type="text/javascript">
   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 line works
               window.open(this.href);
               // this line does not
               links[i].style.color = 'red';
               return false;
            }
         }
      }
   }
   window.onload = doPopups;
</script>


<a href="#" class="change">Use another address</a>