wood burning stoves*
The moose likes HTML, CSS and JavaScript and the fly likes Ternary condition question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Ternary condition question" Watch "Ternary condition question" New topic
Author

Ternary condition question

Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 823
Hi All,
I'm just trying to figure out how the following works :

document.getElementById('textfld').style.display = 'none' ? '' : 'none';

Shouldn't this always hold? It apparently works but I can't figure out how it could. Setting the value of an object looks like the only thing that can be checked and wouldn't that always be true as long as you can do it? Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61447
    
  67

The code makes no sense. The string "none' used as a condition will always evaluate to true.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

Rob Hunter wrote:It apparently works ....


So what does it end up doing? Nothing wrong with the syntax though its just tricky

Just a hand run evaluation,

document.getElementById('textfld').style.display = 'none' ? '' : 'none';
(assignment) = (Condition)?return1:return2

assignment== document.getElementById('textfld').style.display
Condition = 'none' always true
return 1 = ''
return2 'none'

The code is a cryptic way of saying;
document.getElementById('textfld').style.display = ''

Why I have no idea



Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61447
    
  67

I once wrote a COM-based program (back when I wrote shrink-wrap Windows software) to fetch help text from a CICS server and display it on a PC client program. It never displayed the help text, but did accidentally delete the help file on the server.

As it was many pages of code, we always joked that it was the most obfuscated file deletion algorithm in existence.
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 823
Hi guys,
Looked at the code afterwards and you'll see why it looked like it was working. :-)

if(action == "DEL"){
// Firefox browser & IE
document.getElementById('drow').style.display='none' ? '': 'none';
document.getElementById('wrow').style.display='none';
}else{
document.getElementById('wrow').style.display='none' ? '': 'none';
document.getElementById('drow').style.display='none';
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ternary condition question