passing a variable

Discussion in 'Javascript' started by Geoff, Jan 22, 2006.

  1. Geoff

    Geoff Guest

    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>
     
    Geoff, Jan 22, 2006
    #1
    1. Advertising

  2. Geoff

    Hal Rosser Guest

    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.
     
    Hal Rosser, Jan 23, 2006
    #2
    1. Advertising

  3. Geoff

    Geoff Guest

    Thanks guys, that's great!
    "Geoff" <> wrote in message news:dr0g64$hkg$-infra.bt.com...
    As a newbie to javascript can someone tell me please how to pass a variable from HTML to Javascript?
     
    Geoff, Jan 23, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. shil
    Replies:
    2
    Views:
    2,091
    Eliyahu Goldin
    Jan 2, 2007
  2. pereges
    Replies:
    7
    Views:
    464
    pereges
    Jun 1, 2008
  3. Jonas Daunoravicius
    Replies:
    5
    Views:
    337
    Peter
    Jul 17, 2003
  4. Steve Cosh
    Replies:
    1
    Views:
    178
    joewiii
    Nov 18, 2005
  5. Jonas Daunoravicius
    Replies:
    5
    Views:
    311
    Peter
    Jul 17, 2003
Loading...

Share This Page