Hi all, I am a Newbie to Javascript How do I use the if statement with the textbox. when I use the alert(Binary1) I see a 1. alert(Binary1) // this is true - a number 1 for the Binary array. (But when I use the if statement the alert wont fire because it cant tell that Binary1 is a 1) here's the code: if (Binary1 == "1") { alert("I'll stay at home"); } else { alert("I'll go out for a stroll"); } Benn
so try to access it like this if (Binary[1] == "1")// do something if (Binary[0] == "0")// do something
Hi Greatlogix, Its firing the alert now instead of reading the first alert " alert("I'll stay at home")" the if statement runs the ELSE even tho its a 1 hmmmm I try 0 stll it runs the ELSE ifstatement alert? if (Binary[1] == "1")// do something { alert("I'll stay at home"); } else { alert("I'll go out for a stroll2"); } got a clue why? Benn Thanks
If it's a text box, you need to access its value property. if (Binary1.value == 1) { // ... } else { // ... } Since Javascript is weakly typed, you don't need to use quotes to check its value because "1" and 1 are equivalent. I would recommend using either document.getElementById("Binary1") if the text box is not located within a form, otherwise you should use document.forms["FormName"].Binary1 or document.forms[0].Binary1 to access the text box.
Ok I got it to work changing my Binary1 Textbox id="Binary1" value="1001010110" the first number to a 1 runs the else if section changing my Binary1 Textbox id="Binary1" value="0001010110" the first number to a 0 runs the if part - Binary1.value=Message.charAt(0); Var myNum = NULL; myNum = Binary1.value; if (myNum == 1) { alert("I'll Stay home"); } else alert("I'll go out for a stroll"); } </script> if (Binary1 == 1) { If I do this this won't work if (Binary[1] == 1) { If I do this this won't work if (Binary1 == "1") { If I do this this won't work if (Binary[1] == "1") { If I do this this won't work so I replaced the var myNum with Binary1.value works fine...Im happy! happy Memorial Day everybody! Thanks for the Help