Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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 11:20 PM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,688
    Thanks
    80
    Thanked 4,650 Times in 4,612 Posts
    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.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,314
    Thanks
    203
    Thanked 2,565 Times in 2,543 Posts
    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)
    Last edited by Philip M; 10-08-2011 at 09:12 AM.

    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.

  • Users who have thanked Philip M for this post:

    Sayden (10-08-2011)

  • #4
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Yes, thank you, finally a piece of the puzzle. Never realized that you have to put in another bit to get it to work


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •