Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > passing a variable

Reply
Thread Tools

passing a variable

 
 
Geoff
Guest
Posts: n/a
 
      01-22-2006
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 == 12 alert ("This will work OK");
else
alert (" Unfortunately, this will not work");
</script>
</body>
</html>

 
Reply With Quote
 
 
 
 
Hal Rosser
Guest
Posts: n/a
 
      01-23-2006

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.


 
Reply With Quote
 
 
 
 
Geoff
Guest
Posts: n/a
 
      01-23-2006
Thanks guys, that's great!
"Geoff" <(E-Mail Removed)> wrote in message news:dr0g64$hkg$(E-Mail Removed)-infra.bt.com...
As a newbie to javascript can someone tell me please how to pass a variable from HTML to Javascript?


 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
passing copy of a pointer to a variable vs passing the copy pereges C Programming 7 06-01-2008 02:36 PM
Passing a variable value from Javascript to asp.net server variable shil ASP .Net 2 01-02-2007 09:33 PM
passing data from javascript variable to asp variable. Steve Cosh ASP General 1 11-18-2005 04:15 AM
passing javascript variable into asp variable using vbscript Jonas Daunoravicius Javascript 5 07-17-2003 04:54 AM
passing javascript variable into asp variable using vbscript Jonas Daunoravicius ASP General 5 07-17-2003 04:54 AM



Advertisments