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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Outputting Variable?

    Ok so I want the variable total to putput, I have the variable declared in script globally.


    Code:
    var ToppingTotal= 0;
    
    function AddBacon()
    {
    
    
    document.getElementById("BaconAdd").disabled=true;
    document.getElementById("BaconRemove").disabled=false;
    document.getElementById("BaconTick").style.visibility="visible";
    document.getElementById('bacon').src='bacon' + '.png';
    
    
    
    ToppingTotal = (ToppingTotal + 1);
    document.getElementById("ToppingCost").value = ToppingTotal;
    
    
    
    }
    And down in the html:

    Code:
    <input type="text" id="ToppingCost">

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,255
    Thanks
    203
    Thanked 2,557 Times in 2,535 Posts
    How are you calling the function AddBacon()?

    As it stands the textbox does not exist when the function runs.
    It is fine if you have
    <input type = "button" onclick = "AddBacon()">

    That is one reason why it is best to place scripts right ahead of the </body> tag.


    "Knowledge is of no value unless you put it into practice.” - Anton Chekhov (Russian playwright and master of the modern short story, 1860-1904)

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    Zendetr (11-22-2012)

  • #3
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Im calling it from a button

    Code:
    <input id="BaconAdd" onclick="AddBacon()" type="button" value="Add" />
    So how would you suggest I fix it? Its for an assignment so the stress id building
    The text box shows but no value is in it, Im trying to get the total to update in there

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,255
    Thanks
    203
    Thanked 2,557 Times in 2,535 Posts
    Well, this works fine for me. Are there errors in the lines I have omitted? Use alerts to inspect the values. And as Logic Ali would say, use your error console.

    Code:
    <script type = "text/javascript">
    
    var ToppingTotal= 0;
    
    function AddBacon(){
    ToppingTotal +=1;
    document.getElementById("ToppingCost").value = ToppingTotal;
    }
    
    </script>
    
    <input type="text" id="ToppingCost">
    <input type="button"  onclick="AddBacon()"  value="Add Bacon" />
    
    </body>
    Last edited by Philip M; 11-22-2012 at 08:05 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    Zendetr (11-22-2012)

  • #5
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks man, lifesaver the += solved it, the slower way might have some syntax wrong!

    Cheers for that!


  •  

    Posting Permissions

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