0

I'm very new to javascript and currently trying to make a practice project (rock paper scissors)

and is trying to just put all three buttons in one div, get it with javascript, and just use if statements to see which button of the three was clicked

Is this possible? Thank you!

Titsnium
  • 51
  • 5
  • Check [this post](https://stackoverflow.com/questions/1687296/what-is-dom-event-delegation), not much `if`s needed, though. – Teemu Jun 29 '22 at 06:08
  • not impossible dude, yes... you can use dom. like this too https://www.geeksforgeeks.org/rock-paper-and-scissor-game-using-javascript/ – Dimas Wahyu Notonagoro Jun 29 '22 at 06:09
  • thanks! Tho I just decided to use window object to access the variable even outside the function cause it sounds easier for me :D – Titsnium Jun 29 '22 at 06:11
  • Not sure I completely understand but are you looking for event.target to sort out which button has been clicked? – A Haworth Jun 29 '22 at 06:23
  • Please visit the [help], take the [tour] to see what and [ask]. Do some research. If you get stuck, post a [mcve] of your attempt, noting input and expected output using the [\[<>\]](https://meta.stackoverflow.com/questions/358992/ive-been-told-to-create-a-runnable-example-with-stack-snippets-how-do-i-do) snippet editor. – mplungjan Jun 29 '22 at 06:29
  • @Titsnium Please delegate from your div. It is really simple: `document.getElementById("yourDivID").addEventListener("click", function(e) { const tgt = e.target; if (!tgt.matches("button")) return; /* not a button */ const whichButton = tgt.id; if (whichButton === "rock") { /* rock was clicked */ } else if (whichButton === "paper") { /* paper was clicked */ } else if (whichButton === "scissors") { /* scissors was clicked */ } })` – mplungjan Jun 29 '22 at 06:33

0 Answers0