Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Dec 2003
    Location
    America
    Posts
    544
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do i test a text field?

    I have the following function:

    Code:
    function testLauncher(){
    
    var inputBox=document.Bear.babybear.value;
    
      if(inputBox.value==undefined) 
      {
       alert("You chose to not type anything");
       inputBox.value="";
      }
      else
      {
       alert("You wrote, " + inputBox.value);
       inputBox.value="";
      }
    }
    The problem is, that no matter what i type in, i always end up getting the first alert message. How do i basically make it alert one of 2 potential conditions? At first i tried, if (inputBox=="") to make it alert the first alert, but that didnt work either. It seems like the undefined message was all that it would display for me.
    LovesWar

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    There is no such thing as: inputBox.value....
    inputBox is a variable and you already declared its value....

    var inputBox=document.Bear.babybear.value;

    .....Willy

  • #3
    Regular Coder
    Join Date
    Dec 2003
    Location
    America
    Posts
    544
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have no idea what you mean.

    Am i, or am i not to say:

    Code:
    if(inputBox.value==undefined)
    ?

    i thought i need to access the value property to compare whats inside to something else?

    What is wrong with this:

    Code:
    var inputBox=document.Bear.babybear.value;
    ?
    LovesWar

  • #4
    Regular Coder
    Join Date
    Jun 2004
    Location
    underground
    Posts
    186
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What Willy said; i.e.,

    var inputBox=document.Bear.babybear.value;

    if(document.Bear.babybear.value.value==undefined)
    Modesty is often lacking in our transactions.

  • #5
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <script>
    <!--
    
    function testLauncher(){
    
    var inputBox = document.Bear.babybear;
    
     if (inputBox.value == ""){
    alert("You chose to not type anything");
    } else {
    alert("You wrote, " + inputBox.value);
    inputBox.value = "";
    }
    }
    
    //-->
    </script>
    
    <form name="Bear">
    <input type="text" name="babybear" size="10" />
    <input type="button" value="alert" onClick="javascript:testLauncher();" />
    </form>


    undefined
    The value undefined.Core property


    Implemented in
    JavaScript 1.3

    ECMA version
    ECMA-262



    Syntax
    undefined
    Description
    undefined is a top-level property and is not associated with any object.

    A variable that has not been assigned a value is of type undefined. A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value.

    You can use undefined to determine whether a variable has a value. In the following code, the variable x is not defined, and the if statement evaluates to true.

    var x;
    if (x == undefined) {
    alert(x);
    }

    undefined is also a primitive value.

    inputBox, any text field, returns an empty string like:

    <input type="text" name="babybear" value="" size="10" />

    Think of it like:

    if (inputBox.value == ""){
    alert("You chose to not type anything");
    inputBox.value = "";

    you wouldn't write:

    if (inputBox.value == ""){
    alert("You chose to not type anything");
    inputBox.value = undefined;

    Try this:

    Code:
    <script>
    <!--
    
    function testLauncher(){
    
    var inputBox = document.Bear.babybear;
    
      if (inputBox.value == ""){
    alert(typeof(inputBox.value));
    alert("You chose to not type anything");
    } else {
    alert("You wrote, " + inputBox.value);
    inputBox.value = "";
    }
    }
    
    //-->
    </script>
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

  • #6
    Regular Coder
    Join Date
    Dec 2003
    Location
    America
    Posts
    544
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK, i think now i understand what was meant:

    Code:
    What Willy said; i.e.,
    
    var inputBox=document.Bear.babybear.value;
    
    if(document.Bear.babybear.value.value==undefined)
    So when i declare a variable, and assign to it a specific object for its meaning, i shouldnt include within its assignment a specific property of that object/tag? Like you said above, if i include value at the end of babybear, then when i want to access that value property, i would have to do more or less a repeat? Like this:

    Code:
    if(document.Bear.babybear.value.value==undefined)
    ?

    So i should always bear in mind the difference between an object as an assignment, and the accessing of a property through a variable?
    LovesWar

  • #7
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if(document.Bear.babybear.value.value==undefined)
    No. I think what he meant was what you were doing was the equivalent of the above.

    inputBox is a variable and you already declared its value....

    var inputBox=document.Bear.babybear.value;

    So, you would simply say:


    if(inputBox== "")

    Not only is value.value redundant, but improper syntax.



    -james
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •