...

View Full Version : condition quick syntax help



khan8822
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'
}
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?

SpirtOfGrandeur
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.

Brandoe85
07-06-2005, 07:13 PM
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.....

martin_narg
07-06-2005, 10:40 PM
(a==b) ? doIfTrue : doIfFalse;

Harry Armadillo
07-07-2005, 04:40 AM
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...

glenngv
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum