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 10-08-2011, 03:30 AM   PM User | #1
Sayden
New to the CF scene

 
Join Date: Oct 2011
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Sayden is an unknown quantity at this point
Need help with form to var value

I'm very new at Javascript and I can not understand what I am doing wrong. I need to enter a value into a form and have the value entered carried over to become a variable value, for a check I want it to return a document.write with the value entered.

Code:
<html>
<head>
<title>part 1</title>
<body>
<script type="text/javascript">
function numbercheck ()
{
var enteredChar = document.nbr.number.value;
document.write("your number is" + enteredChar);
}






</script>
</head>
<form name="nbr" action="" method="get">
Number: <input type="text" name="number" value="" />
<input type="submit" value="Submit" />
</form>

</body>
</html>

Last edited by Sayden; 10-08-2011 at 10:20 PM..
Sayden is offline   Reply With Quote
Old 10-08-2011, 05:42 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,567
Thanks: 62
Thanked 4,057 Times in 4,026 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
You can't do it.

If you use document.write ANY time after a page is fully loaded into the browser, it WIPES OUT *EVERYTHING* on that page! Even the javascript that did the document.write.

YOu will need to learn to use DOM methods, instead.

Not only that... You never even *call* your numbercheck function.

Not only that... If you use a submit button, then the page *will* be submitted and the screen will be wiped out and the page reloaded from the browser.

In short...keep trying. You have a ways to go. But not a mile, maybe a meter or two.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Old 10-08-2011, 08:10 AM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Old Pedant View Post
You can't do it.

If you use document.write ANY time after a page is fully loaded into the browser, it WIPES OUT *EVERYTHING* on that page! Even the javascript that did the document.write.
Yes, but that does not matter in this context. The real trouble is that the function is never called.

Code:
<html>
<head>
<title>part 1</title>
<body>
<script type="text/javascript">
function numbercheck () {
var enteredChar = document.nbr.number.value;
alert ("Your number is " + enteredChar);
document.write("Your number is " + enteredChar);
}
</script>
</head>
<body>

<form name="nbr" action="" method="get" onsubmit = "numbercheck()" >
Number: <input type="text" name="number" value="" />
<input type="submit" value="Submit" />
</form>

</body>
</html>
Sayden - I expect you realise that for practical use you will need to check that the value entered ins in fact a number.


"Copy from one book, it’s called plagiarism; copy from three, it’s called research." -- Wilson Mizner (1876-1933)
__________________

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; 10-08-2011 at 08:12 AM..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
Sayden (10-08-2011)
Old 10-08-2011, 10:18 PM   PM User | #4
Sayden
New to the CF scene

 
Join Date: Oct 2011
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Sayden is an unknown quantity at this point
Yes, thank you, finally a piece of the puzzle. Never realized that you have to put in another bit to get it to work
Sayden is offline   Reply With Quote
Reply

Bookmarks

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 09:50 PM.


Advertisement
Log in to turn off these ads.