jug Posted July 2, 2007 Share Posted July 2, 2007 Not a brillaint javascript coder but have been trying to do an IF within an IF. Surely its possible and im just hoping its just a simple syntax problem. All this code does is declare variables from html objects, checks whether some of them have values and if they do finds out the highest value of two variables and decalres another varibale with this highest value. Any response would be much appreciated. Thanks in advance Jug var inamount1 = document.comboform.line1inputbox.value var outamount1 = document.comboform.line1display.value var inamount2 = document.comboform.line2inputbox.value var outamount2 = document.comboform.line2display.value var inamount3 = document.comboform.line3inputbox.value var outamount3 = document.comboform.line3display.value var inamount4 = document.comboform.line4inputbox.value var outamount4 = document.comboform.line4display.value var inamount5 = document.comboform.line5inputbox.value var outamount5 = document.comboform.line5display.value if (inamount1 != ""){ if (inamount1 > outamount1){var line1take = inamount1} if (outamount1 > inamount1){var line1take = outamount1} } if (inamount2 != ""){ if (inamount2 > outamount2){var line2take = inamount2} if (outamount2 > inamount2){var line2take = outamount2} } if (inamount3 != ""){ if (inamount3 > outamount3){var line3take = inamount3} if (outamount3 > inamount3){var line3take = outamount3} } if (inamount4 != ""){ if (inamount4 > outamount4){var line4take = inamount4} if (outamount4 > inamount4){var line4take = outamount4} } if (inamount5 != ""){ if (inamount5 > outamount5){var line5take = inamount5} if (outamount5 > inamount5){var line5take = outamount5} } Quote Link to comment Share on other sites More sharing options...
kael.shipman Posted July 2, 2007 Share Posted July 2, 2007 Works fine for me. Have you tried setting constant test values instead of using form fields? I just did the following test with code copied from your post and it output the expected result: <script type="text/javascript"> var inamount1 = 23; var outamount1 = 66; var inamount2 = 12; var outamount2 = 1; if (inamount1 != ""){ if (inamount1 > outamount1){var line1take = inamount1} if (outamount1 > inamount1){var line1take = outamount1} } if (inamount2 != ""){ if (inamount2 > outamount2){var line2take = inamount2} if (outamount2 > inamount2){var line2take = outamount2} } document.write(line1take+" "+line2take); //output "66 12" </script> Quote Link to comment Share on other sites More sharing options...
emehrkay Posted July 3, 2007 Share Posted July 3, 2007 save yourself some code var line1take = (inamount1 > outamount1) ? inamount1 : outamount1; line1intake is inamount1 if inamount1 is more than outamount1, else it is outamout1 Quote Link to comment Share on other sites More sharing options...
jug Posted July 3, 2007 Author Share Posted July 3, 2007 Thanks guys the problem has now been solved. Using var line1take = (inamount1 > outamount1) ? inamount1 : outamount1; that emehrkay supplied and i was able to recode the lines so i didnt need an IF within an IF. The new code looked like this var line1take = (inamount1 > outamount1) ? inamount1 : outamount1; if (inamount1 == ""){var line1take = 0} line1take = Number(line1take) and nows works perfectly. Thanks again this forum is always great when youre stuck. Jug Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.