Home > Mobile >  Checking if any button is clicked in javascript
Checking if any button is clicked in javascript

Time:08-14

I am working at a rubik's cube html site and I ought to mention I am a beginner. What I want to do now is to check if any piece (I have created 54 divs with the same class) is clicked and if so to change its color (modifying the div's background). I ask you if there is any method that can make my task easier or at least give me a piece of advice. Thank you in advance!

CodePudding user response:

as a simple method, you could write a function to accept the reference to a div and change its color whenever a div is clicked and call it on each div's onClick method.

Code:

<html>
  <body>
  <div id="div1" onClick="changeMyColor(this)">
  Here is the div, click on this to change the color
  </div>

    <script>
      function changeMyColor(div) {
        div.style.backgroundColor = 'green';
      }
    </script>
    </body>
</html>

CodePudding user response:

You can use some keyboard/mouse events. Check them out : https://www.w3schools.com/jsref/event_onkeydown.asp

  • Related