Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
127
rated 0 times [  130] [ 3]  / answers: 1 / hits: 18068  / 13 Years ago, tue, march 22, 2011, 12:00:00

I can't use else in my script; how can I implement an if else statement without using else?


I'm trying to resize a div:


function hideTable(){
var table = document.getElementById('PDemo');
if(table.style.width == "50%") table.style.width = "150px";
if(table.style.width == "150px") table.style.width = "50%";
}

This script doesn't work because as soon as the first if statement is executed, the condition for the second if statement becomes true, so that executes too, and thus nothing changes.


More From » javascript

 Answers
4

If you can't use an else (and that is crazy), use a switch.



switch (table.style.width) {

case '50%':
...
break;

case '150px':
...
break;

}

[#93140] Monday, March 21, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
estefanib

Total Points: 508
Total Questions: 104
Total Answers: 83

Location: Lebanon
Member since Sun, Aug 2, 2020
4 Years ago
;