...

View Full Version : Resolved Need help with form to var value



Sayden
10-08-2011, 04:30 AM
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.



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

Old Pedant
10-08-2011, 06:42 AM
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.

Philip M
10-08-2011, 09:10 AM
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.


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

Sayden
10-08-2011, 11:18 PM
Yes, thank you, finally a piece of the puzzle. Never realized that you have to put in another bit to get it to work



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum