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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post How do I get the value from a text box

    Can you tell me why it won't work or what will work.
    This is what I want to do:

    1. When I click on the submit button
    2. Get the value inputed into the text box by the user
    3. If the value in the text box is equal to "small"
    4. alert("The value equals small");

    This is my codeWhy won't it work)

    JavaScript:
    <script type="text/javascript">
    var checkValue = document.getElementById('text').value;
    function checkNameOfVideo()
    {
    if(checkValue == "small"){
    alert("The value equal small");
    }
    }
    </script>

    HTML:
    <form>
    <label for="text"><input id="text" type="text" name="text" placeholder="GoPro Video Search" required></label>
    <label><input id="submit" type="button" name="submit" tabindex="5" value="Search" onClick="checkNameOfVideo"></label>
    </form>

    Thanks Stuart

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    1,2,3,4 is what you wanted to do, but you did 2,1,3,4

    Move the "getting" of the value into the function and it will do the trick.

  • #3
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The code is free of error i think.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,159
    Thanks
    203
    Thanked 2,548 Times in 2,526 Posts
    Quote Originally Posted by Hawaii View Post
    The code is free of error i think.
    You ought not to make such rash pronouncments. There are two major errors.

    <script type="text/javascript">
    var checkValue = document.getElementById('text').value;

    As devnull has pointed out, the OP is trying to capture the value of the variable checkValue at the moment the script runs, on page load. At that moment the element "text" does not exist. Even if it did it would be "" (blank).

    He has also missed out the brackets in onClick="checkNameOfVideo()"

    Note also that "Small" or "sMaLL or "SMALL" is not the same as "small". The comparison should be with the input value .toLowerCase()


    “A man ceases to be a beginner in any given science and becomes a master in that science when he has
    learned that he is going to be a beginner all his life.” Robin G. Collingwood (English Philosopher, 1889-1943)
    Last edited by Philip M; 11-15-2011 at 08:18 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Tags for this Thread

    Posting Permissions

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