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 6 of 6
  1. #1
    New Coder
    Join Date
    Jul 2005
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    condition quick syntax help

    I want to convert

    Code:
    if(document.getElementById('BD').style.display == 'none')
    
    		   	       {
    		   	       document.getElementById('BD').style.display = '';
    		   	       document.myImage1.src = 'sminus.gif'
    		   	       }
    		   	       else
    		   	       {
    		   	       document.getElementById('BD').style.display = 'none';
    		   	       document.myImage1.src = 'splus.gif'
    		   			}
    to this style

    condition ? do1, do2 : do1, do2

    can someone help me on the syntax?

  • #2
    Regular Coder
    Join Date
    May 2005
    Location
    Michigan, USA
    Posts
    566
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Personally, that syntax is the ugliest thing ever. And it is horrible that JS is allowed to be written that way.

  • #3
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    If you want it that way, you might as well learn what everything means. Do a search for javascript ternary operator. Should give you the idea.....

  • #4
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Code:
    (a==b) ? doIfTrue : doIfFalse;
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper

  • #5
    Regular Coder
    Join Date
    Feb 2005
    Posts
    400
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    var foo=document.getElementById('BD').style;
    document.myImage1.src = (foo.display == 'none')?'sminus.gif':'splus.gif';
    foo.display = (foo.display == 'none')?'':'none';
    Slower, harder to read than the original...

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    It is ok to use the ternary operator if the if and else blocks each contain only one statement. But for multiple statements as in this case, you should not use ternary operator. As Harry demonstrated, the condition (foo.display == 'none') is evaluated twice when ternary operator is used. Obviously, this is not efficient.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

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