Matching Game

 <!DOCTYPE html>

<button id="btn1">Button 1</button>

<button id="btn1">Button 1</button>

<button id="btn2">Button 2</button>

<button id="btn2">Button 2</button>


<script>

var buttons = document.querySelectorAll("button");

var hiddenButton = null;  // Track the hidden button


for (var i = 0; i < buttons.length; i++) {

buttons[i].onclick = function() {

if (hiddenButton) {

if (hiddenButton.id === this.id) {

hiddenButton.disabled = true;

this.disabled = true;

hiddenButton.style.visibility = "visible";

hiddenButton = null;

alert("✅ Match Found!");

} else {

hiddenButton.style.visibility = "visible";

hiddenButton = null;

alert("❌ Not a Match");

}

} else {

this.style.visibility = "hidden";

hiddenButton = this;

}

};

}

</script>


<!DOCTYPE html>

<button id="btn1">Button 1</button>

<button id="btn1">Button 1</button>

<button id="btn2">Button 2</button>

<button id="btn2">Button 2</button>


<script>

var btns = document.querySelectorAll("button");

var buttons = null;  // Track the hidden button


for (var i = 0; i < btns.length; i++) {

btns[i].onclick = function() {

if (buttons) {

if (buttons.id === this.id) {

buttons.disabled = true;

this.disabled = true;

buttons.style.visibility = "visible";

buttons = null;

alert("✅ Match Found!");

} else {

buttons.style.visibility = "visible";

buttons = null;

alert("❌ Not a Match");

}

} else {

this.style.visibility = "hidden";

buttons = this;

}

};

}

</script>




Comments

Popular posts from this blog

cc app code button match with size return with Try Again Button

AS2 in JavaScript add score 1 press button A and B clicked id null

ClaudieAi Pair Game WITHOUT JS DOM OBJECT Direct access by ID ✅ ✔✔🌹🌹🎉