0

I have a code that increases a variable "money" when I press the key "S" and I would like to change the value when I press a button but it doesnt work.

Thanks.

Increases the "money":

document.addEventListener('keydown', function(event) {
console.log("Event");
if(event.keyCode == 83) {
console.log("Key Pressed");
document.getElementById("key").innerHTML = "<img src=\"stick2.gif\">";
shoot -= 1;
document.getElementById("shoot").innerHTML = shoot;
if(shoot > 0){
money += 4;
document.getElementById("money").innerHTML = money;
document.getElementById("cash").innerHTML = "<img src=\"money.png\">";
    }
  }
}

Changes the value:

    function onClick12()   {
    document.getElementById("ak").innerHTML = "<img src=\"ak/ak1.png\">";
    document.getElementById('b1').disable = true;
    if(event.keyCode == 83 && shoot > 0) {
    money += 8;
    document.getElementById("money").innerHTML = money;
    document.getElementById("cash").innerHTML = "<img src=\"money.png\">";
      }
    }
    function onClick13()   {
    document.getElementById("ak").innerHTML = "<img src=\"ak/ak4.png\">";
    document.getElementById('b4').disable = true;
    if(event.keyCode == 83 && shoot > 0) {
    money += 12;
    document.getElementById("money").innerHTML = money;
    document.getElementById("cash").innerHTML = "<img src=\"money.png\">";
        }
      }

Buttons:

<input type="button" onclick="onClick12()" id="b1" value="Use">
<input type="button" onclick="onClick13()" id="b4" value="Use">
3
  • You wish to change the money variable ? Can you try replacing the functions names : function onClick13(event) {...} function onClick12(event) {...}
    – Rigin
    Feb 2, 2016 at 10:28
  • Ok I will try it thanks !
    – Pedro D.
    Feb 2, 2016 at 10:29
  • I think i got a mistake - You are clicking a button right, WHY you need event.keyCode in that button click ? You could have removed this check right?
    – Rigin
    Feb 2, 2016 at 10:36

1 Answer 1

1
if(event.keyCode == 83 && shoot > 0)

The variable event is not defined in the scope of you function onClick12/13 Note that if you still get the event datas from the onclick event you won't have a keycode since click event don't embbed keybaord information.

But do you really want your user to press a key and click for what you want ? If so i suggest you to have catch a keydown event to register in a variable the key currently pressed and a keyup event to reset that value.

That variable should be defined outside the function so the other function can access it. Here is the basic stuff

var key83Pressed = false;
document.addEventListener('keydown', function(event) {
    key83Pressed = event.keyCode == 83;


}
function onCLick12(){
    if(key83Pressed){

    }
} 
document.addEventListener('keyup', function(event){

    if(event.keyCode==83){
          key83Pressed  = false;
    }

});
2
  • If I make an other keydown event inside onClick function will it work
    – Pedro D.
    Feb 2, 2016 at 10:26
  • Nope this isn't how it works. that means that every time you will click you will bind a new listener listening for key events. This means that this won't work until you have click at least once on the button and you will have a memory leak.
    – Walfrat
    Feb 2, 2016 at 10:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.