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 5 of 5
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    33
    Thanks
    1
    Thanked 1 Time in 1 Post

    Post How to put a JavaScript variable into the value of a text box

    I have this code but it doesnt work:

    Code:
    <script type="text/javascript">
    var number = 10;
    document.getelementbyid(number).value = number;
    </script>
    <input type="text" id="number" value="">
    Can anybody help me out here?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    A lot of errors in so few lines!

    a) You are using the same name/id for a Javascript variable and an HTML element.

    b) Javascript is case sensitive so you must spell getElementById exactly as shown

    c) The id of the textbox is a literal so must be in quotes. Without the quotes the reference is to the variable number, which has the value of 10, which is nonsense. (Just in case - an id must start with a letter).

    d) Your script attempts to refer to the textbox before it exists.

    Code:
    <input type="text" id="mynumber" value="">
    
    <script type="text/javascript">
    var number = 10;
    document.getElementById("mynumber").value = number;
    </script>

    Quizmaster: In astronomy, a nucleus, a coma and a tail are parts of which celestial body?
    Contestant: A horse.
    Last edited by Philip M; 03-26-2012 at 04:37 PM.

    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.

  • #3
    New Coder
    Join Date
    Mar 2012
    Posts
    33
    Thanks
    1
    Thanked 1 Time in 1 Post
    Thanks works perfectly.

    Sorry about the errors lol.


  • #4
    New Coder
    Join Date
    Sep 2011
    Location
    United Kingdom
    Posts
    74
    Thanks
    2
    Thanked 3 Times in 3 Posts
    There is an error in your syntax . Capital letters are important.

    Code:
    <script type="text/javascript">
    var number1 = 10;
    var number = document.getElementById("number");
    //  number.value = "Hello World";   //  <-- This does not work jet.
    </script>
    <input type="text" id="number">
    But I could not assign the value jet.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    clausrei -


    Note that in Internet Explorer, names and id's are global variables and thus you should NEVER use a (global) variable or function name which is the same as an HTML element name or id. Example:-

    Code:
    <input id = "text">
    
    <script type = "text/javascript">
    text = "Hello World";  // global variable
    document.getElementById('text').value = text;
    </script>
    I agree that the variable here is local because it is declared with the var keyword. But you are playing with fire! Follow my simple rule.

    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.


  •  

    Tags for this Thread

    Posting Permissions

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