Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-09-2012, 08:52 PM   PM User | #1
yaseenyahya
New to the CF scene

 
Join Date: Oct 2012
Posts: 9
Thanks: 0
Thanked 1 Time in 1 Post
yaseenyahya is an unknown quantity at this point
Any Shortest Way

I have a 5 text in white color in <anchor tag> when i click 1 it changes to "orange" And when i click 2...except this 2 al changes to white..and so on...is there any shortest way to do it or another way.. =)
Code:
window.onload = function(){
  var as = document.getElementsByTagName("a");

   as[0].onclick = function(){
    this.style.color = "orange";
    as[1].style.color = "white";
    as[2].style.color = "white";
    as[3].style.color = "white";
    as[4].style.color = "white";
    }
    as[1].onclick = function(){
    this.style.color = "orange";
    as[0].style.color = "white";
    as[2].style.color = "white";
    as[3].style.color = "white";
    as[4].style.color = "white";
  }
  as[2].onclick = function(){
    this.style.color = "orange";
    as[0].style.color = "white";
    as[1].style.color = "white";
    as[3].style.color = "white";
    as[4].style.color = "white";
  }
  as[3].onclick = function(){
    this.style.color = "orange";
    as[0].style.color = "white";
    as[2].style.color = "white";
    as[1].style.color = "white";
    as[4].style.color = "white";
}
as[4].onclick = function(){
    this.style.color = "orange";
    as[0].style.color = "white";
    as[2].style.color = "white";
    as[3].style.color = "white";
    as[1].style.color = "white";
}

};
yaseenyahya is offline   Reply With Quote
Old 11-09-2012, 09:22 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Well, for starters, if you moved that code to *AFTER* the bulk of the HTML, to just before the </body>, then you wouldn't need the window.onload.

Like this:
Code:
<html>
<body>
<a ... >
<a ... >
<a ...>
<a ...>

<script type="text/javascript">
(
  function( ) /* anonymous master function */
  {
      var as = document.getElementsByTagName("a");     
      for ( var a = 0; a < as.length; ++a )
      {
          as[a].onclick = 
              function( )
              {
                  // change all but the one clicked on to white
                  for ( var n = 0; n < as.length; ++n )
                  {
                       as[n].style.color = ( as[n] == this ) ? "orange" : "white";
                  }
             } // end of onclick function

      } // end of for loop

  } // end of anonymous master function

)( ); // self-invoked the master function
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 11-09-2012, 11:51 PM   PM User | #3
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 959
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
Quote:
Originally Posted by yaseenyahya View Post
is there any shortest way to do it or another way.. =)
Code:
<script type="text/javascript">

(function() /* Place BELOW all <a> tags */
{
  var as = document.getElementsByTagName("a");

  for( var i = 0; as[ i ]; i++ )
    as[ i ].onclick = f;

  function f()
  {
    for( var i = 0; as[ i ]; i++ )
      as[ i ].style.color = ( as[ i ] === this ? "orange" : "white" );   
  }
})();

</script>
Logic Ali is offline   Reply With Quote
Old 11-09-2012, 11:55 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Slow day, LogicAli? <grin/>
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 11-10-2012, 12:09 AM   PM User | #5
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 959
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
Quote:
Originally Posted by Old Pedant View Post
Slow day, LogicAli? <grin/>
No it's memory-saving day. Don't generate multiple function bodies when one is adequate.
Logic Ali is offline   Reply With Quote
Old 11-10-2012, 12:13 AM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Oh, I see your point.

Funny thing is, when I write code for myself I almost always do it the way you showed, even if the function will only be called once.

I prefer the self-documenting nature of the resultant code.

But when I post in the forums, I tend to write the shortest code rather than necessarily most efficient. Mostly because I'm lazy, I guess.

Anyway, you should have pointed out WHY you rewrote my code, to educate the poster(s). Glad you did so now.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:27 PM.


Advertisement
Log in to turn off these ads.