Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-22-2012, 06:11 PM   PM User | #1
Zendetr
New to the CF scene

 
Join Date: Nov 2012
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
Zendetr is an unknown quantity at this point
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">
Zendetr is offline   Reply With Quote
Old 11-22-2012, 06:41 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
Zendetr (11-22-2012)
Old 11-22-2012, 06:48 PM   PM User | #3
Zendetr
New to the CF scene

 
Join Date: Nov 2012
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
Zendetr is an unknown quantity at this point
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
Zendetr is offline   Reply With Quote
Old 11-22-2012, 07:02 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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>
__________________

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.

Last edited by Philip M; 11-22-2012 at 07:05 PM..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
Zendetr (11-22-2012)
Old 11-22-2012, 09:20 PM   PM User | #5
Zendetr
New to the CF scene

 
Join Date: Nov 2012
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
Zendetr is an unknown quantity at this point
Thanks man, lifesaver the += solved it, the slower way might have some syntax wrong!

Cheers for that!
Zendetr is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:04 PM.


Advertisement
Log in to turn off these ads.