JavaScript - Shorthand For Else If?
hello,
I have googled (I promise) but I can't find any examples... basically my code is getting bogged down in if/else if/ else statements, and I am wondering if there is a shorthand. I know (I think) that you can do if/else like this: variable = (condition) ? trueValue : falseValue; but can you add an else if into the code? thanks in advance. Similar TutorialsHey, this problem confused me, I don't know if it can be done, but it would be convenient for me if yes. Suppose we have this Code: var bool = false; var test = ['a', 'b', (bool ? 'c' : ('k', 'f')), 'd']; array test then will be ["a", "b", "f", "d"], but i want ["a", "b", 'k'", f", "d"]. Does anybody know is there a way to do that? Thanks! As is in order to trigger the text turning back to black it seems I need to put the lines like below one by one for all the id's. I'm wondering if there is a less lengthy way to get this done? // JavaScript Document function CheckTheForm() { var result = true; var msg=""; document.getElementById('maincontactemail').style.color='#000000'; document.getElementById('maincontact').style.color='#000000'; heres a sample code: Code: if (bba==true) { ta=' a4 '; if (cca==true){ tb = ' 1x ' ;versions(ta,tb);} if (ccb==true){ tb = ' 2x ' ;versions(ta,tb);} if (ccc==true){ tb = ' 3x ' ;versions(ta,tb);} if (ccd==true){ tb = ' 5x ' ;versions(ta,tb);} } if (bbb==true) { ta=' a3 '; if (cca==true){ tb = ' 1x ' ;versions(ta,tb);} if (ccb==true){ tb = ' 2x ' ;versions(ta,tb);} if (ccc==true){ tb = ' 3x ' ;versions(ta,tb);} if (ccd==true){ tb = ' 5x ' ;versions(ta,tb);} } if (bbc==true) { ta=' a2 '; if (cca==true){ tb = ' 1x ' ;versions(ta,tb);} if (ccb==true){ tb = ' 2x ' ;versions(ta,tb);} if (ccc==true){ tb = ' 3x ' ;versions(ta,tb);} if (ccd==true){ tb = ' 5x ' ;versions(ta,tb);} } any way i could make it shorter? this is just sample code, i dont know, but i may have to write 70/3 times this code..... |