View Full Version : condition quick syntax help
07-06-2005, 06:38 PM
I want to convert
if(document.getElementById('BD').style.display == 'none')
document.getElementById('BD').style.display = '';
document.myImage1.src = 'sminus.gif'
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?
07-06-2005, 07:09 PM
Personally, that syntax is the ugliest thing ever. And it is horrible that JS is allowed to be written that way.
07-06-2005, 07:13 PM
07-06-2005, 10:40 PM
(a==b) ? doIfTrue : doIfFalse;
07-07-2005, 04:40 AM
document.myImage1.src = (foo.display == 'none')?'sminus.gif':'splus.gif';
foo.display = (foo.display == 'none')?'':'none';Slower, harder to read than the original...
07-07-2005, 06:20 AM
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.
Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.