...

View Full Version : remember number



sherlockturtle
07-29-2011, 01:38 AM
I need help on some code
function makefarm(){
document.getElementById("farms").value ++ ;
document.getElementById("pop").value -=2 ;
document.getElementById("coin").value -=10 ;
document.getElementById("lumber").value -=20 ;
document.getElementById("CIfarm").value ++ ;
document.getElementById("CIfarm").value ++ ;
document.getElementById("CIfarm").value ++ ;
document.getElementById("CIfarm").value ++ ;
document.getElementById("CIfarm").value ++ ;
}
function upgradefarm(){
document.getElementById("iron").value -=5 ;
document.getElementById("coin").value -=20 ;
document.getElementById("farmupgrades").value ++ ;
}
var food = function () {
var f1 = document.getElementById('buttonhere');
var f2 = '<input type="button" value="Make Farm" onclick="makefarm()" /> Farms:<input type="text" id="farms" size="1" value=0 style="background-color:transparent;border:0px solid white;" READONLY/> <input type="button" value="Upgrade Farm" onclick="upgradefarm()" /> Farm Upgrades:<input type="text" id="farmupgrades" size="1" value=0 style="background-color:transparent;border:0px solid white;" READONLY/> <br/><br/><br/> Current Income:<input type="text" id="CIfarm" size="1" value=0 style="background-color:transparent;border:0px solid white;" READONLY/>';
f1.innerHTML = f2;
}
Coin:<input type="text" id="coin" value=50 size="1" style="background-color:transparent;border:0px solid white;" READONLY />
Lumber:<input type="text" id="lumber" value=100 size="1" style="background-color:transparent;border:0px solid white;" READONLY />
Iron:<input type="text" id="iron" value=25 size="1" style="background-color:transparent;border:0px solid white;" READONLY />
Food:<input type="text" id="food" value=100 size="1" style="background-color:transparent;border:0px solid white;" READONLY />
POP:<input type="text" id="pop" value=10 size="1" style="background-color:transparent;border:0px solid white;" READONLY />
so if i press the makefarm() button it will add to the numbers but if i press it again they will got to zero how could i make it remember the numbers?(the input boxes are the second code)

jmrker
07-29-2011, 02:26 AM
I don't know what you are trying to accomplish
and I don't know what you expect to happen, :confused:
but I do know that the code posted will not work as is: :eek:

This is my best guess...:D


<!DOCTYPE HTML>
<html>
<head>
<title> Untitled </title>
<style type="text/css">
.inp { background-color:transparent;border:0px solid white; }
</style>

<script type="text/javascript">
function makefarm(){
document.getElementById("farms").value ++ ;
document.getElementById("pop").value -=2 ;
document.getElementById("coin").value -=10 ;
document.getElementById("lumber").value -=20 ;
document.getElementById("CIfarm").value ++ ;
document.getElementById("CIfarm").value ++ ;
document.getElementById("CIfarm").value ++ ;
document.getElementById("CIfarm").value ++ ;
document.getElementById("CIfarm").value ++ ;
}
function upgradefarm(){
document.getElementById("iron").value -=5 ;
document.getElementById("coin").value -=20 ;
document.getElementById("farmupgrades").value ++ ;
}
var food = function () {
var f1 = document.getElementById('buttonhere');
var f2 = '<input type="button" value="Make Farm" onclick="makefarm()" />';
f2 +='Farms:<input type="text" id="farms" size="1" value=0 class="inp" READONLY/>';
f2 +=' <input type="button" value="Upgrade Farm" onclick="upgradefarm()" />';
f2 +=' Farm Upgrades:';
f2 +=' <input type="text" id="farmupgrades" size="1" value=0 class="inp" READONLY/>';
f2 +=' <br/><br/><br/> ';
f2 +=' Current Income:';
f2 +=' <input type="text" id="CIfarm" size="1" value=0 class="inp" READONLY/>';
f1.innerHTML = f2;
}
window.onload = function() { food(); }
</script>

</head>
<body>
Coin:<input type="text" id="coin" value=50 size="1" class="inp" READONLY />
Lumber:<input type="text" id="lumber" value=100 size="1" class="inp" READONLY />
Iron:<input type="text" id="iron" value=25 size="1" class="inp" READONLY />
Food:<input type="text" id="food" value=100 size="1" class="inp" READONLY />
POP:<input type="text" id="pop" value=10 size="1" class="inp" READONLY />
<p>
<div id="buttonhere"></div>
</body>
</html>

sherlockturtle
07-29-2011, 02:33 AM
Ok sorry this is what im trying to acomplish
<script type="text/javascript">
var makeButton = function () {
var e = document.getElementById('buttonhere');
var h = '<input type="button" value="Click me!" onclick=""/>';
e.innerHTML = h;
}

</script>

<br /><input type="button" value="Click me to create button" onclick="makeButton();return false;" />

<br /><span id="buttonhere"></span>


This makes a button correct? then when the button that made is pressed it would add to a input.

<script type="text/javascript">
function plus{
document.getelememtnById("input").value ++;

}
var makeButton = function () {
var e = document.getElementById('buttonhere');
var h = '<input type="button" value="Click me!" onclick="plus"/>';
e.innerHTML = h;
}

</script>

<br /><input type="button" value="Click me to create button" onclick="makeButton();return false;" />

<br /><span id="buttonhere"></span>

<input type="text" value=0 id="input" />

so if you pressed the button that was made it would increment input but if you pressed the make button again(or if your press a other make button) it would make the input value = 0 how could you make it remember its one?

jmrker
07-29-2011, 05:08 AM
That is not the way to create a dynamic button.
Look into 'createElement()' function for more information.
(See: http://www.w3schools.com/dom/met_document_createelement.asp
and other links with a google search)

The way you are currently doing it, you will only get one button
followed by many duplicates that only replace the original one button created.
Not what you want to do, I believe.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum