Hello and welcome to our community! Is this your first visit?
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

    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)

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

    <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>

    Thanks Stuart

  2. #2
    Senior Coder
    Join Date
    Dec 2010
    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. #3
    New to the CF scene
    Join Date
    Nov 2011
    The code is free of error i think.

  4. #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    London, England
    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