MZainT
MZainT

Reputation: 1

Calculator keydown

I'm new to javascript and I've got an assignment to create an calculator with javascript. Now I've ran into a struggle which I'm hoping someone can help me with.

I've made an eventlistner on the calculator where i can press the buttons through 'click'. Although i also need to have the function for keydown which i can't understand for somereason.

How can i get an eventListener for keydown where the value of the pressed key becomes an operand or operator on the display?

(my buttons have been given the name "knappar" and my display is declaired "display".

Upvotes: 0

Views: 465

Answers (1)

Rednirug
Rednirug

Reputation: 81

It helps to post snippets of code so we can better understand what you're asking. You should try looking up how event listeners work either through documentation or videos.

document.getElementById("myBtn").addEventListener("click", displayDate)

A quick little explanation: The button in the HTML has an ID of "myBtn", so we use the "document.getElementById" to select it. Then we use addEventListener to make it listen for a "click". After the event happens, it do whatever we tell it to. In this case, we tell it to run "displayDate".

Try checking out w3schools documentation. It has simple examples and lets you mess with the code a little bit.

Upvotes: 2

Related Questions