By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,327 Members | 2,661 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,327 IT Pros & Developers. It's quick & easy.

passing a variable

P: n/a
As a newbie to javascript can someone tell me please how to pass a variable from HTML to Javascript?

I have the following script which works OK if I input to str from the "prompt" box, but I want to input from a one-line text box on an HTML page.

Thanks in advance.

Geoff.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>New Page 1</title>
</head>
<body>
<script type = "text/javascript">
// Input number
var str = null;
if ( str == null );
var str = prompt( "Enter number", "" );
var num2 = str.substr(0,3);
var res = +num2;
// Test for correctness
if(res == 123) alert ("This will work OK");
else
if(res == 124) alert ("This will work OK");
else
if(res == 128) alert ("This will work OK");
else
alert (" Unfortunately, this will not work");
</script>
</body>
</html>

Jan 22 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Geoff wrote:
As a newbie to javascript can someone tell me please how to pass a
variable from HTML to Javascript?

I have the following script which works OK if I input to str from the
"prompt" box, but I want to input from a one-line text box on an HTML
page.

You have to use the DOM to locate the element (hint -give it a name or
id attribute), then read it's value.

--
Ian Collins.
Jan 22 '06 #2

P: n/a

Assume the text box is named "inBox" and it is in a form named "form1"

var theValue = document.form1.inBox.value;
or
var theValue = document.forms[0].elements[0].value; //assuming the text box
is the first element in the form.

You'll probably want to add a button to generate a onclick so you can call
the function.
Jan 23 '06 #3

P: n/a
Thanks guys, that's great!
"Geoff" <so*****@somewhere.sometime> wrote in message news:dr**********@nwrdmz03.dmz.ncs.ea.ibs-infra.bt.com...
As a newbie to javascript can someone tell me please how to pass a variable from HTML to Javascript?
Jan 23 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.