-1

I am trying to add an image to my screen at the press of the "enter" key, but am unable to do so. I am currently using Raphael JavaScript library. I tried to use event.which == 13 but it does not work. There was no error on the console.

picture of code

Alexander Nied
  • 12,804
  • 4
  • 25
  • 45
help
  • 1
  • 2
  • Does this answer your question? [Execute JS code after pressing the spacebar](https://stackoverflow.com/questions/24386354/execute-js-code-after-pressing-the-spacebar) – nicael Apr 03 '22 at 16:50
  • `.style.display` does not have a property value of `visible`. Instead set the value to `block` or `inline-block`. But if the element in question really is of `visibility` then use `.style.visibility = "visible"`. – mardubbles Apr 03 '22 at 16:51
  • [Please do not upload images of code when asking a question](https://meta.stackoverflow.com/q/285551). – Alexander Nied Apr 14 '22 at 04:14

1 Answers1

1

The following code pattern should work for you. I will have a div with some text (you can replace this with your image) and I will assign an event handler of keypress to the whole document body.

document.body.addEventListener("keypress", function(ev) {

// note that we use 'ev' as opposed to 'event', since we named the param 'ev'.

    if (ev.which === 13) {
        document.getElementById("img").style.display = "block";
    }

}); 
<div id="img" style="display:none">Hidden Image</div>

Basically the event is added to the document body so we know it will trigger regardless of the user's focus. But you can change this to the particular element later during your tests.

Also it is important to use the parameter name you passed in consistently to the event handler. So when passing in ev, use spelling ev as opposed to some other name (e.g. event) when you make use of it in the handler function.

One last note is from comments on when I said:

.style.display does not have a property value of visible. Instead set the value to block or inline-block. But if the element in question really is of visibility then use .style.visibility = "visible"

These corrections hopefully fix your problem or provide you with insights to get it solved.

mardubbles
  • 129
  • 6