...

View Full Version : Help with javascript code



tannerbeck50
11-17-2010, 10:30 PM
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]

BIOSTALL
11-17-2010, 10:40 PM
<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.

DrDOS
11-17-2010, 10:57 PM
Another version:
<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>

Philip M
11-17-2010, 11:33 PM
<input type="text" name="number" size="4" value=a>

Places the literal character a in the textbox.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum