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
    Nov 2010
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Help with javascript code

    Hey guys,
    Im kind of a newb and i need help with this dice game im trying to build to help me learn javascript.
    If you open up the code up in your browser and click the roll button, it will clear everything and write var a in the top left corner.
    What i am trying to do is write the var a into the text box.
    any suggestions??




    [CODE]
    <html>
    <head>
    <script type="text/javascript">


    </script>
    <style type="text/css">
    body
    {
    background-image: url(http://projectraw.files.wordpress.com/2010/02/background_brick_wall.jpg);
    }
    h1
    {
    font-family:"sans-serif",tahoma;
    background-repeat: no-repeat;
    color:white;
    }
    p
    {
    color:white;
    font-family:"sans-serif",tahoma;
    font-weight:900;
    }
    </style>
    </head>
    <body>
    <center><h1>Ghetto Dice Project</h1></center>
    <center>
    <p>
    Hand coded with care by Tanner Beck
    </p>
    </br>
    </br>
    </br>
    </br>
    </br>
    </br>
    </br>
    </br>
    </br>
    </br>
    <p>
    <center>
    <table BGCOLOR="#888888" BORDER="3" CELLPADDING="5">
    <tr>
    <input type="text" name="number" size="4" value=a>
    </tr>
    </br>
    <tr>
    <button onclick="
    a=(Math.floor(Math.random()*13));
    document.write(a)">ROLL</button>
    </tr>
    </table>
    </br>
    </center>
    </p>
    </body>
    </html>
    [CODE]

  • #2
    New Coder
    Join Date
    Apr 2010
    Posts
    45
    Thanks
    0
    Thanked 8 Times in 8 Posts
    Code:
    <input type="text" name="number" id="number" size="4" value="a">
    <button onclick="a=(Math.floor(Math.random()*13)); document.getElementById('number').value=a">ROLL</button>
    Something like so? It's referencing the input field by an id (in this case 'number') and setting the variable 'a' as the value.
    BIOSTALL.com - Web Development Snippets, Hints and Tips

  • Users who have thanked BIOSTALL for this post:

    tannerbeck50 (11-17-2010)

  • #3
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,974
    Thanks
    15
    Thanked 229 Times in 229 Posts
    Another version:
    Code:
    <table BGCOLOR="#888888" BORDER="3" CELLPADDING="5">
    <tr>
    <form name="mynum">
    <input type="text" name="number" size="4" value=a>
    </form>
    </tr>
    </br>
    <tr>
    <button onclick="
    a=(Math.floor(Math.random()*13));alert(a);
    document.mynum.number.value=(a);">ROLL</button>
    </tr>
    </table>

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,986
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    <input type="text" name="number" size="4" value=a>

    Places the literal character a in the textbox.


  •  

    Posting Permissions

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