1

I want to add 10 to 0 every time I click a button and show it on the screen. I've tried everything but I can't get it to work.

This belongs to a longer code:

var bet = 0;
pen.fillText('Bet: ' + bet, 350, 390);

function reactToBetIncrease() {
    bet += 10;
}
4
  • 2
    It would be helpful if you would include the portion of code where you actually call the function reactToBetIncrease(). The calling context will matter a lot in your case. Is the bet variable a global (part of the window object)? Commented Dec 5, 2012 at 11:19
  • perhaps you need to display the new value as well? You don't seem to do that. Commented Dec 5, 2012 at 11:19
  • It is not clear to me what you are trying to achieve. Commented Dec 5, 2012 at 11:20
  • I'm new to this, sorry. My actual code is longer. Commented Dec 5, 2012 at 11:20

2 Answers 2

3
var bet=0;

pen.fillText('Bet: '+bet,350,390);

function reactToBetIncrease()

{

    bet += 10;
    pen.fillText('Bet: '+bet,350,390);
}
Sign up to request clarification or add additional context in comments.

7 Comments

Another question. When I click the button the numbers overlap, how do I remove the old one?
pen.font = '20pt Calibri'; pen.fillText('Inzet: '+inzet,350,390);
this is very simple in generic, it's done like this: element.value+=10;
var pen=c.getContext("2d");
oh you are using canvas, that's another question, I don't know much about canvas
|
0

HTML

<button id="myButton">

JavaScript

var bet = 0;
$("#myButton").click(function() { bet += 10; });

Note: I don't know what your pen object is, nor what library it's from. It may require a single call on update - in which case it should live in the click callback. Otherwise it may require a call each frame. Since you haven't specified exactly what it does, I've left this out.

3 Comments

note that this alone won't affect the display in any way. It's still useful if attaching the handler was a problem.
@JanDvorak - I have no idea what pen is. And the question seemed like the handler was the problem, that's why I answered it.
And yet if it was polling the variable (bad idea) then most likely it wouldn't be passed a string generated from the variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.