if statement Textbox.value = 1

Discussion in 'JavaScript' started by lbenn, May 22, 2008.

  1. #1
    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
     
    lbenn, May 22, 2008 IP
  2. greatlogix

    greatlogix Active Member

    Messages:
    664
    Likes Received:
    13
    Best Answers:
    1
    Trophy Points:
    85
    #2
    Is Binary1 an array? what value it holds?
     
    greatlogix, May 23, 2008 IP
  3. lbenn

    lbenn Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Binary is a Textbox and it holds the number 1 or 0
    Benn
     
    lbenn, May 23, 2008 IP
  4. greatlogix

    greatlogix Active Member

    Messages:
    664
    Likes Received:
    13
    Best Answers:
    1
    Trophy Points:
    85
    #4
    so try to access it like this

    if (Binary[1] == "1")// do something
    if (Binary[0] == "0")// do something
     
    greatlogix, May 23, 2008 IP
  5. lbenn

    lbenn Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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
     
    lbenn, May 23, 2008 IP
  6. vpguy

    vpguy Guest

    Messages:
    275
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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.
     
    vpguy, May 24, 2008 IP
  7. lbenn

    lbenn Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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
     
    lbenn, May 24, 2008 IP