Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I let one element do two events at the same time

so, I’m having a game, and I want the user to reset the score when he clicks the button, and reset boundaries when he hovers over it.
How can I do it so that he can both, hover and click

function reset_bounderies() {


    let start = document.getElementById("start")
    start.addEventListener("mouseover", function (event) {
        game = true;
        document.querySelectorAll(".boundary:not(.example)").forEach(item => {
            item.classList.remove("youlose")
            document.getElementById("status").innerHTML = `Begin by moving your mouse over the "S".`
        })
    })
    start();

}

and the second function

function reset_game() {

    let start = document.getElementById("start")
    start.addEventListener("click", function (event) {
        game = true;
        score = 0;
        print_score()
        
    })
    start();
}

P.S: I can’t edit html or css file

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can add two differents EventListener to your button

function functionOne(){
  console.log("functionOne")
}

function functionTwo(){
  console.log("functionTwo")
}

const myButton = document.getElementById("myButton")

myButton.addEventListener('click', functionOne)
myButton.addEventListener('mouseover', functionTwo)
<button id="myButton">Click me !</button>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading