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

Thread: remember number

  1. #1
    Regular Coder
    Join Date
    May 2011
    Posts
    357
    Thanks
    23
    Thanked 1 Time in 1 Post

    remember number

    I need help on some code
    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;
    }
    Code:
    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)
    Last edited by sherlockturtle; 07-29-2011 at 01:42 AM.

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,075
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb

    I don't know what you are trying to accomplish
    and I don't know what you expect to happen,
    but I do know that the code posted will not work as is:

    This is my best guess...
    Code:
    <!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>

  • #3
    Regular Coder
    Join Date
    May 2011
    Posts
    357
    Thanks
    23
    Thanked 1 Time in 1 Post
    Ok sorry this is what im trying to acomplish
    Code:
    <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.
    Code:
    <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?

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,075
    Thanks
    38
    Thanked 498 Times in 492 Posts
    That is not the way to create a dynamic button.
    Look into 'createElement()' function for more information.
    (See: http://www.w3schools.com/dom/met_doc...ateelement.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.


  •  

    Posting Permissions

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