dimanche 6 janvier 2019

How to check for a multiplication sign in JavaScript?

I'm working on a calculator program. I'd like to display the multiplication sign in HTML but also be able to detect in JavaScript whether or not the multiplication sign was clicked, so it doesn't get added to the display. This applies to other symbols. I tried using the UNICODE value, but if you press (for example):

1,

then 2,

then the multiplication sign,

the multiplication sign shows up in the display (which I don't want). Any ideas?

const myDiv = document.querySelector('#display');
const myButtons = document.querySelectorAll('button');
myButtons.forEach((button) => {
    button.addEventListener('click', () => {
        if(button.textContent == 0x000D7) {
            // call on some operator function
        } else {
            myDiv.textContent += button.textContent;
        }
    });
});
<div id=display></div>
<button>1</button>
<button>2</button>
<button>&times;</button>



    

Aucun commentaire:

Enregistrer un commentaire