Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-14-2011, 08:25 PM   PM User | #1
stuwestgate
New to the CF scene

 
Join Date: Nov 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
stuwestgate is an unknown quantity at this point
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
stuwestgate is offline   Reply With Quote
Old 11-14-2011, 08:43 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
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.
devnull69 is offline   Reply With Quote
Old 11-15-2011, 07:35 AM   PM User | #3
Hawaii
New to the CF scene

 
Join Date: Nov 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Hawaii is an unknown quantity at this point
The code is free of error i think.
Hawaii is offline   Reply With Quote
Old 11-15-2011, 07:57 AM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,105
Thanks: 197
Thanked 2,422 Times in 2,400 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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)
__________________

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.

Last edited by Philip M; 11-15-2011 at 08:18 AM..
Philip M is offline   Reply With Quote
Reply

Bookmarks

Tags
javascript, submit, text, textbox

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:52 AM.


Advertisement
Log in to turn off these ads.