By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,934 Members | 1,528 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,934 IT Pros & Developers. It's quick & easy.

Beginner Needing Help!!

P: n/a
I want this text field to always display a number variable.
The variable "num" is defined in the head, how can I get a text box to
display this varibale??

<input name="Balance" type="text" value="num" size="8" maxlength="8">

Cheers
Jul 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
<script language="javascript">
var num = 5; // 5 or variable
</script>
<body onload="javascript:document.myform.Balance.value = num;">
<form name="myform">
<input name="Balance" type="text" value="" size="8" maxlength="8">
</form>
</body>
This is how i'd do it.

Don't forget JS is case sensitive.

Stu

"TempMan" <do***@tpg.com.au> wrote in message
news:14**************************@posting.google.c om...
I want this text field to always display a number variable.
The variable "num" is defined in the head, how can I get a text box to
display this varibale??

<input name="Balance" type="text" value="num" size="8" maxlength="8">

Cheers

Jul 20 '05 #2

P: n/a
Leave off the javascript: prefix and that will do fine.

"Stuart Palmer" <tr**********@youcant.com> wrote in message
news:8F******************@newsfep2-gui.server.ntli.net...
| <script language="javascript">
| var num = 5; // 5 or variable
| </script>
| <body onload="javascript:document.myform.Balance.value = num;">
| <form name="myform">
| <input name="Balance" type="text" value="" size="8" maxlength="8">
| </form>
| </body>
|
|
| This is how i'd do it.
|
| Don't forget JS is case sensitive.
|
| Stu
|
| "TempMan" <do***@tpg.com.au> wrote in message
| news:14**************************@posting.google.c om...
| > I want this text field to always display a number variable.
| > The variable "num" is defined in the head, how can I get a text box to
| > display this varibale??
| >
| > <input name="Balance" type="text" value="num" size="8" maxlength="8">
| >
| > Cheers
|
|
Jul 20 '05 #3

P: n/a
do***@tpg.com.au (TempMan) writes:
I want this text field to always display a number variable.
The variable "num" is defined in the head, how can I get a text box to
display this varibale??

<input name="Balance" type="text" value="num" size="8" maxlength="8">


You have to add the value using Javascript. The code for this is:

document.forms['formName'].elements['Balance'].value = num;

You can either use this code anywhere after the element tag:

<script type="text/javascript">
document.forms['formName'].elements['Balance'].value = num;
</script>

(Remember to use the "type" attribute, not the "language" attribute
on the script tag, if writing HTML 4 or later).

Or you can add it using the documents onload handler:

<body
onload="document.forms['formName'].elements['Balance'].value = num;">

(don't put "javascript:" in front of HTML event handlers, it is not
a link, so the javascript: pseudo protocol doesn't apply).

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4

P: n/a
None of these work.
It's a game, the user opens the page then clicks on the amount they wish to
deposit.
I need the text box to display the amount in their bank. It will be $0 when
the page loads and them change as they play the game..

Below is the full code (which isn't bug free yet..)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>:: Tafe - Roll a Dice ::</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<!-- Casion Emulator -->

<script language="JavaScript" type="text/javascript">
<!-- Hide me from old browsers

// Gets bg colour based on time
time = new Date()
if (time.getHours() <= 12) {
document.bgColor = "CC0000" }
else {
document.bgColor = "339900"
}

// Casion Images
function pic(){
pic_array=new Array()
pic_array[1]="images/pic01.gif"
pic_array[2]="images/pic02.gif"
pic_array[3]="images/pic03.gif"
pic_array[4]="images/pic04.gif"
pic_array[5]="images/pic05.gif"
pic_array[6]="images/pic06.gif"

picnum=Math.floor(Math.random()*pic_array.length)
if (picnum==0)
picnum=1
document.write('<center><img src="'+pic_array[picnum]+'"
border=0></center><br>')
}
pic()
document.write("<hr>")
// Game starts here

var bank = 0
var betbank = 0
var money_in = 0
var money_out = 0
var bet_out = ""
var bet_in = 0
var theroll = 0
var win = 0
var rollnum = 0
var dep_clickcount = 0
var gamble_clickcount = 0
var pick_clickcount = 0
var roll_clickcount = 0

// Prompt for deposit
function deposit(money_in) {

if ( dep_clickcount == 0 ) {
alert( "You Deposited $" + money_in )
bank = bank + money_in
dep_clickcount++
}
else { alert("You can only make one deposit, hit 'refresh' if you wish to
change your deposit")
}
}

// Prompt for money on bet
function gamble(money_out)
{
if ( bank != 0 && bank >= money_out ) {
gamble_clickcount++
bank = bank - money_out
betbank = betbank + money_out
alert("$" + betbank + " placed on bet. $" + bank + " left in the bank") }
else {
alert("Balance: $" + bank ) }
}

// Prompt for bet
function bet(bet_in)
{
if ( bet_in == 1 ) {
bet_out = "1 to 4" }
else {
if ( bet_in == 2 ) {
bet_out = "5 to 8" }
else {
bet_out = "9 to 12" }
}

if ( pick_clickcount == 0 && dep_clickcount == 1 && gamble_clickcount >=
1 ) {
alert("You Picked " + bet_out)
pick_clickcount++
}
else {
if ( dep_clickcount == 0 ) {
alert("You must deposit money first") }
else { if ( gamble_clickcount == 0 ) {
alert("You must bet first") }
else { alert("You have already picked")
}
}
}

}

// Rolls the dice

function get_random()
{
var ranNum= Math.round(Math.random()*11);
return ranNum;
}

function roll()
{
if ( pick_clickcount == 1 && dep_clickcount == 1 && roll_clickcount == 0 &&
bank != 0 ) {
alert("You are betting $" + betbank + " on " + bet_out )

theroll = get_random();

dicenum = new Array(11)
dicenum[0]=1;
dicenum[1]=2;
dicenum[2]=3;
dicenum[3]=4;
dicenum[4]=5;
dicenum[5]=6;
dicenum[6]=7;
dicenum[7]=8;
dicenum[8]=9;
dicenum[9]=10;
dicenum[10]=11;
dicenum[11]=12;

rollnum = dicenum[theroll]
alert("Roll: " + rollnum)

roll_clickcount = 0
}
else {
if ( dep_clickcount == 0 ) {
alert("You must deposit money first")
}
else {
if ( pick_clickcount == 0 ) {
alert("You must bet first")
}
else {
if ( roll_clickcount == 1 ){
alert("You have already rolled")
}
else {
if ( bank == 0 && roll_clickcount == 0 ){
alert("Game Over")
}
else {
if ( bank == 0 ){
alert("Last Go")
}
else { alert("Place another bet") }
}
}
}
}
}

if ( bet_in == 1 && rollnum >= 1 && rollnum <= 4 ) {
bank = bank + ( betbank * 2 )
win = betbank * 2
alert( "You won: $" + win )
alert( "1 Bank $" + bank ) //debugging
win = 0 }
else {
if ( bet_in == 2 && rollnum >= 5 && rollnum <= 8 ) {
bank = bank + ( betbank * 2 )
win = betbank * 2
alert( "You won: $" + win )
alert( "2 Bank $" + bank ) //debugging
win = 0
betbank = 0
pick_clickcount = 0
roll_clickcount = 0 }
else {
if ( bet_in == 3 && rollnum >= 9 && rollnum <= 12 ) {
bank = bank + ( betbank * 2 )
win = betbank * 2
alert( "You won: $" + win )
alert( "3 Bank $" + bank ) //debugging
win = 0
betbank = 0
pick_clickcount = 0
roll_clickcount = 0 }
else {
alert( "You lost: $" + betbank )
alert( "Bank $" + bank ) //debugging
betbank = 0
win = 0
pick_clickcount = 0
roll_clickcount = 0
}
}
}
}
// end hide -->
</script>
</head>
<body link="#FFFFFF" vlink="#FFFFFF" alink="#FFFFFF"
onload="document.bal.Balance.value = bank;">
Click the amout you wish to deposit
<br>
<IMG onmouseover="window.status='$500'" onclick="deposit(500)"
onmouseout="window.status='';return true" height=75 src="images/0500.gif"
width=200>
<IMG onmouseover="window.status='$600'" onclick="deposit(600)"
onmouseout="window.status='';return true" height=75 src="images/0600.gif"
width=200>
<IMG onmouseover="window.status='$700'" onclick="deposit(700)"
onmouseout="window.status='';return true" height=75 src="images/0700.gif"
width=200><br>
<IMG onmouseover="window.status='$800'" onclick="deposit(800)"
onmouseout="window.status='';return true" height=75 src="images/0800.gif"
width=200>
<IMG onmouseover="window.status='$900'" onclick="deposit(900)"
onmouseout="window.status='';return true" height=75 src="images/0900.gif"
width=200>
<IMG onmouseover="window.status='$1000'" onclick="deposit(1000)"
onmouseout="window.status='';return true" height=75 src="images/1000.gif"
width=200>
<br>
<hr>
<br>
<form name="bal">
Current Balance: $<input name="Balance" type="text" value="" size="8"
maxlength="8">
</form>
<form>
Place Money On Bet:
<input type="button" name="Submit00" value="$100" onClick="gamble(100)">
<input type="button" name="Submit01" value="$200" onClick="gamble(200)">
</form>
<form>
Place Bet:
<input type="button" name="Submit02" value="1 to 4" onClick="bet(1)">
<input type="button" name="Submit03" value="5 to 8" onClick="bet(2)">
<input type="button" name="Submit03" value="9 to 12" onClick="bet(3)">
</form>
<form>
<input type="button" name="Submit04" value="Roll Dice" onClick="roll()">
</form>
<br>
<p align="center"><a href="casino.html"><font size="5" face="Comic Sans
MS">Return to casino</font></a></p>
</body>
</html>

Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.