• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Ternary condition question

 
Rob Hunter
Ranch Hand
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64832
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code makes no sense. The string "none' used as a condition will always evaluate to true.
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64832
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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';
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic