[thelist] onclick - unobtrusive javascript

Tim Burgan email at timburgan.com
Fri Jul 22 23:22:53 CDT 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>





More information about the thelist mailing list