<!-- https://jsfiddle.net/Twisty/crdxcg90/3/
https://codepen.io/joelkit98/pen/aLErpx -->
<style>
* {
box-sizing: border-box;
}
body {
font-family: sans-serif;
}
.box-container {
background: #ccc;
padding: 1em;
display: inline-block;
vertical-align: top;
}
.box {
display: inline-block;
vertical-align: top;
width: 60px;
height: 60px;
background: #000;
color: #fff;
font-size: 2em;
text-align: center;
transition: transform 0.15s ease;
border-radius: 50%;
padding-top: 0.3em;
}
.droparea1 {
background: yellow;
width: 400px;
height: 200px;
display: inline-block;
vertical-align: top;
text-align: center;
}
.score-container {
display: inline-block;
vertical-align: top;
border: 3px solid #000;
font-size: 2em;
padding: 1em;
}
.box.correct {
background: green;
}
.box.incorrect {
background: red;
}
.box.ui-draggable-dragging {
box-shadow: 10px 20px 30px rgba(0,0,0,0.5);
transform: scale(2);
}
/* Enable jquery UI */
</style>
<script>
window.console = window.console || function(t) {};
</script>
<script>
if (document.location.search.match(/type=embed/gi)) {
window.parent.postMessage("resize", "*");
}
</script>
</head>
<body translate="no" >
<div class="box-container">
<div class="box" data-numtype="odd">1</div>
<div class="box" data-numtype="even">2</div>
<div class="box" data-numtype="odd">3</div>
<div class="box" data-numtype="even">4</div>
<div class="box" data-numtype="odd">5</div>
<div class="box" data-numtype="even">6</div>
</div>
<div class="droparea1" data-area-numtype="odd">
<h2>Drop <u>odd</u> boxes here</h2>
</div>
<div class="droparea1" data-area-numtype="even">
<h2>Drop <u>even</u> boxes here</h2>
</div>
<div class="score-container">
Score: <span id="score">0</span>
</div>
<script src="https://cpwebassets.codepen.io/assets/common/stopExecutionOnTimeout-1b93190375e9ccc259df3a57c1abc0e64599724ae30d7ea4c6877eb615f89387.js"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js'></script>
<script id="rendered-js" >
$(document).ready(function () {
// Vars
var score = 0;
var numBoxesDropped = 0;
var numBoxes = $('.box').length;
// Creat sound object
var correctSound = document.createElement('audio');
correctSound.setAttribute('src', 'https://ssl.gstatic.com/dictionary/static/pronunciation/2022-03-02/audio/co/correct_en_us_1.mp3');
var errorSound = document.createElement('audio');
errorSound.setAttribute('src', 'https://ssl.gstatic.com/dictionary/static/pronunciation/2022-03-02/audio/wr/wrong_en_us_1.mp3');
//Make boxes draggable
$('.box').draggable({
revert: true });
// make droparea droppable
$('.droparea1').droppable({
accept: '.box',
drop: handleBoxDrop });
// function that handles the box being droppped
function handleBoxDrop(event, ui) {
// alert("you dropped a box");
// vars
var box = ui.draggable;
var boxNumType = box.attr('data-numtype');
//$(this) = what is inside of?
var dropArea1 = $(this);
var dropArea1NumType = dropArea1.attr('data-area-numtype');
// Check if box number type matches number type of drop area
if (boxNumType == dropArea1NumType) {
// num type matches!
box.addClass('correct');
correctSound.play();
score++; //give points
} else {
// num type does NOT match!
box.addClass('incorrect');
errorSound.play();
}
//disable dragging
box.draggable('disable').draggable('option', 'revert', disable);
numBoxesDropped++;
console.log(numBoxesDropped);
// output score
$('#score').text(score);
// check if game has ended
// if(numBoxesDropped == 4){
// game finished!
// alert("Game Finished!");
// }
if (numBoxesDropped == numBoxes) {
// game finished!
alert("game finished!");
}
}
});
//# sourceURL=pen.js
</script>
</body>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Drag and Drop Game</title>
<style>
body { font-family: Arial, sans-serif; text-align: center; }
.container { display: flex; justify-content: center; margin-top: 50px; }
.draggable-item {
width: 100px; height: 100px; margin: 10px;
background-color: lightblue; display: flex;
align-items: center; justify-content: center;
border: 2px solid black; cursor: grab;
}
.drop-zone {
width: 120px; height: 120px; margin: 10px;
border: 2px dashed black; display: flex;
align-items: center; justify-content: center;
}
.correct { border-color: green; background-color: lightgreen; }
.wrong { border-color: red; background-color: pink; }
</style>
</head>
<body>
<h2>Drag and Drop Game</h2>
<p>Drag the correct item to the right box!</p>
<div class="container">
<div id="item1" class="draggable-item" draggable="true" data-match="zone1">A</div>
<div id="item2" class="draggable-item" draggable="true" data-match="zone2">B</div>
</div>
<div class="container">
<div id="zone1" class="drop-zone" data-match="item1">Drop A Here</div>
<div id="zone2" class="drop-zone" data-match="item2">Drop B Here</div>
</div>
<script>
const items = document.querySelectorAll('.draggable-item');
const zones = document.querySelectorAll('.drop-zone');
items.forEach(item => {
item.addEventListener('dragstart', (event) => {
event.dataTransfer.setData('text/plain', event.target.id);
});
});
zones.forEach(zone => {
zone.addEventListener('dragover', (event) => {
event.preventDefault();
});
zone.addEventListener('drop', (event) => {
event.preventDefault();
const itemId = event.dataTransfer.getData('text/plain');
const draggedItem = document.getElementById(itemId);
if (draggedItem.dataset.match === zone.id) {
zone.appendChild(draggedItem);
zone.classList.add('correct');
} else {
zone.classList.add('wrong');
setTimeout(() => zone.classList.remove('wrong'), 1000);
}
});
});
</script>
</body>
</html>
---------------------------------------------------------------------------------------------------------------------
IMAGE DRAG AND DROP IN JAVASCRIPT
Read More
http://fbgadgets.blogspot.com/2014/09/drag-and-drop-practice-game-with-image.html
---------------------------------------------------------------------------------------------------------------------
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<style>
a#skipnav{font-family:Arial,Helvetica,sans-serif; font-size:.65em; text-decoration:none; float:right; padding-right:3em}
/*page-styletype*/
.section, .practice{background:#fff; border-bottom:.1em solid #CCC; border-top:.1em solid #E5E5E5; border-left:.1em solid #E5E5E5; border-right:.1em solid #CCC; margin-top:.8em}
#sectionOne{background:#fff; border-bottom:.1em solid #CCC; border-top:.1em solid #E5E5E5; border-left:.1em solid #E5E5E5; border-right:.1em solid #CCC; margin-top:.8em}
ol, ul{padding:0em 0em 0em 3em}
ol li, ul li{font-family:Arial,Helvetica,sans-serif; font-size:.8em; color:#4D4D4D; line-height:2em}
ul.biblio li{text-indent:-5em; margin-left:4em; margin-bottom: 0em; list-style-type:none; font-size:.74em}
.gridContainer.clearfix #grammar .table50.bdr .asub td ul{margin-left:2em; margin-top:0em; margin-bottom:0em; padding-left:.4em}
.gridContainer.clearfix #grammar .table50.bdr .asub td ul ul{margin-left:1.2em; margin-top:0em; margin-bottom:0em; padding-left:.4em}
.gridContainer.clearfix #grammar .table50.bdr .asub td ul li, #grammar .table50.bdr .asub td ul li ul li{font-size:1em}
.borderleft{border-left:.12em solid #D5DCE6}
.borderbottom{border-bottom:.112em solid #D5DCE6}
.border tr td, .border{border-bottom:.07em solid #CCC; border-right:.07em solid #CCC; border-spacing:0}
.bdr tr td{border-left:.12em solid #D5DCE6}
.floatright{float:right; margin:0 .5em 0 0; padding:0 0 0 1em}
.floatleft{float:left; padding-left:.5em; margin:0 1em 0 0}
.clearfloat{clear:both; height:0; font-size:.06em; line-height:0}
.clear{clear:both; width:100%; height:.1em; margin:0:padding:0}
#focusLf, .focusleft, #focusRt, .focusright, #focus, .focus{background-image:url(img/bkgline.gif); border-bottom:.20em solid #D5DCE6; border-top:.09em solid #DDE5F0; border-left:.1em solid #D5DCE6; border-right:.15em solid #D5DCE6; margin-top:.8em}
#focTitle, .foctitle{font-weight:700; font-size:.68em; color:#FFF; font-family:Arial,Helvetica,sans-serif; background-color:#7288B7; padding:.5em}
#Exp, .exp{font-family:Arial,Helvetica,sans-serif; font-size:.70em; color:#3B5998; margin:0; padding:.8em 1em 1em .8em; line-height:1.4}
#Eg, .eg{padding:.15em 0 }
#Eg p, .eg p{padding:.4em .8em; margin:0}
</style>
<style>
/* Add some margin to the page and set a default font and colour */
/* Slots for final card positions */
#cardSlots, #cardPile {
margin: 1em auto 0 auto;
clear:none; float:left;
/*border: .125em solid #666;*/
}
/* The initial pile of unsorted cards */
#cardSlots, #cardPile {
width: 61em;
height: 9.5em;
padding: .02em 0 0 .5em;
margin: 1em auto;
}
/* Individual cards and slots */
#cardPile div {
float: left;
width: 6.5em;
height: 7.75em;
clear:none; float:left; margin-left:0; display:block;
padding: 0em;
border: .125em solid #ccc;
-moz-border-radius: .535em;
-webkit-border-radius: .535em;
border-radius: .535em;
margin: 0 0 0 .5em;
background: #fff;
}
#cardSlots div {
clear:none; float:left; margin-left:0; display:block;
width: 8.5em;
height: 9.75em;
padding: 0em;
border: .125em solid #666;
-moz-border-radius: .535em;
-webkit-border-radius: .535em;
border-radius: .535em;
margin: 0 0 0 .65em ;
background: #fff;
text-align: center;
}
#cardSlots div:first-child, #cardPile div:first-child {
margin-left: 0;
}
#cardSlots div.hovered {
background: #aaa;
}
#cardSlots div {border-style:dashed; font-family:Arial, Helvetica, sans-serif; font-size:.8em}
#cardPile div {
background: #fff;
color: #000;
font-size: 1em;
text-shadow: 0 0 3px #666;
}
#cardPile div img {
display: block;
margin-left: auto;
margin-right: auto }
#cardPile div.ui-draggable-dragging {
-moz-box-shadow: 0 0 .5em rgba(0, 0, 0, .8);
-webkit-box-shadow: 0 0 .5em rgba(0, 0, 0, .8);
box-shadow: 0 0 .5em rgba(0, 0, 0, .8);
}
/* Correct card border */
#card1.correct, #card2.correct, #card3.correct, #card4.correct, #card5.correct, #card6.correct, #card7.correct, #card8.correct { border-color:#3F5F9E; border-width:medium;}
/* "You did it!" message */
#successMessage {
position: absolute;
left: 20em;
top: 130em;
width: 10em;
height: 4em;
z-index: 100;
background: #FFF;
border: 2px solid #333;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
-moz-box-shadow: .3em .3em .5em rgba(0, 0, 0, .8);
-webkit-box-shadow: .3em .3em .5em rgba(0, 0, 0, .8);
box-shadow: .3em .3em .5em rgba(0, 0, 0, .8);
padding: 15px;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js"></script>
<script type="text/javascript">
var correctCards = 0;
$( init );
function init() {
// Hide the success message
$('#successMessage').hide();
$('#successMessage').css( {
left: '10em',
top: '100em',
width: 0,
height: 0
} );
// Reset the game
correctCards = 0;
$('#cardPile').html( '' );
$('#cardSlots').html( '' );
// Create the pile of shuffled cards
// Create the pile of shuffled cards
var numbers = [ 1, 2, 3, 4, 5, 6 ];
var imgs = [
'https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhPcEWSJ3Nu4Qr6VsIhzW_mEJ1XNzv5U_TN-vBMTn0p-hodEF3VjPeN3rns7cO2-ShDyOfiZK_riivZbmH6yCfmQYB6ao5ASyLoKQagRw9Z11qibmuBNph012Iob9-_IKWMpTlAJ4J2PxsP/s1600/book1.gif',
'https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVH_vBkxjeGeOr_JAzPB8OnzbahzTCqbwnTLT7jG7zZSe-GJVWJ4bRVVYB5oBQ8RydaRqGmATMZV6e5bgLtq86MvKFQkR-OvKNisOLLPq9FtiQBGWlVGhVdX95GWPGcMkGwUKJ3QPpGmOK/s1600/book2.gif',
'https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiBCed8Rzfkv_Fi7Nt71iHhU6ZxNUyNHWuN5ZiwLr1tHzz-yctSNmZvyeQ7Rx0DLAk1mL809WqZgBrjWsNB_LzG_ciSDzCzbvKXO2RGKV-eOS4oUMJkmmRJitI24DQJHoNcTyS3Z1ChvWQB/s1600/book3.gif',
'https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEibD8A2kMAlh2QaMbD9edthhBWJtcmBemstfi00IHRrNPJe3oO_urpTvVG_cTOWaq7FnT3QSTceqtgCKKxK3i3_dbVcyhelkg6FtabfD_QH0o3ZwauPjtJKAp3RS4LSdlmavppocyKLa74r/s1600/book4.gif',
'https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgjCj3e5G-DiA5KRRDIyDH0D5EQStZFK3mL1RgFG7huE_BsTNni0hXVHDWvph8OjXqz64zoCt34AIt1ej_IhLds_sm4q03Dry1kAoXyUCsGuu-pZ-nudMiRH1Htp_YuchBXMqBt81ufWASA/s1600/book5.gif',
'https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUGaafq53HE0HQ-LqXkXmC8H6X4XU0WzNIQb2Lmm-G6YlJ4qPbm-hhJvNtBgk8rm9FxI0EkgzJONnpHmdOz0Njufkw_hbhM06nHVYbqNBbULcGq_olE2UcI7DZD2ON7doOLsWrMYeyuiKO/s1600/book6.gif'
];
numbers.sort( function() { return Math.random() - .5 } );
for ( var i=0; i<6; i++ ) {
$('<div><img src="' + imgs[numbers[i] - 1] + '" alt="alt"/></div>').data( 'number', numbers[i] ).attr( 'id',
'card'+numbers[i] ).appendTo( '#cardPile' ).draggable( {
containment: '#content',
stack: '#cardPile div',
cursor: 'move',
revert: true
} );
}
// Create the card slots
var words = [ 'first', 'second', 'third', 'fourth', 'fifth', 'sixth' ];
for ( var i=1; i<=6; i++ ) { $('<div>' + words[i-1] + '</div>').data( 'number', i ).appendTo( '#cardSlots' ).droppable( {
accept: '#cardPile div',
hoverClass: 'hovered',
drop: handleCardDrop
} );
}
}
function handleCardDrop( event, ui ) {
var slotNumber = $(this).data( 'number' );
var cardNumber = ui.draggable.data( 'number' );
// If the card was dropped to the correct slot and prevent it being dragged again
if ( slotNumber == cardNumber ) {
ui.draggable.addClass( 'correct' );
ui.draggable.draggable( 'disable' );
$(this).droppable( 'disable' );
ui.draggable.position( { of: $(this), my: 'left top', at: 'left top' } );
ui.draggable.draggable( 'option', 'revert', false );
correctCards++;
}
// If all the cards have been placed correctly then display a message and next button
if ( correctCards == 6 ) {
$('#successMessage').show();
$('#successMessage').animate( {
top: '34em',
width: '120em',
width: '150em',
height: '5em',
opacity: 1
} );
$(document).ready(function(){
$('#sound_tag')[0].play();
});
}
}
</script>
</head>
<body>
<div class="gridContainer clearfix">
<div id="sectionOne">
<div id="cardPile">
<div id="card1" class="ui-draggable" style="position:relative">
<img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhPcEWSJ3Nu4Qr6VsIhzW_mEJ1XNzv5U_TN-vBMTn0p-hodEF3VjPeN3rns7cO2-ShDyOfiZK_riivZbmH6yCfmQYB6ao5ASyLoKQagRw9Z11qibmuBNph012Iob9-_IKWMpTlAJ4J2PxsP/s1600/book1.gif"></div>
<div id="card2" class="ui-draggable" style="position:relative">
<img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVH_vBkxjeGeOr_JAzPB8OnzbahzTCqbwnTLT7jG7zZSe-GJVWJ4bRVVYB5oBQ8RydaRqGmATMZV6e5bgLtq86MvKFQkR-OvKNisOLLPq9FtiQBGWlVGhVdX95GWPGcMkGwUKJ3QPpGmOK/s1600/book2.gif"></div>
<div id="card3" class="ui-draggable" style="position:relative">
<img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiBCed8Rzfkv_Fi7Nt71iHhU6ZxNUyNHWuN5ZiwLr1tHzz-yctSNmZvyeQ7Rx0DLAk1mL809WqZgBrjWsNB_LzG_ciSDzCzbvKXO2RGKV-eOS4oUMJkmmRJitI24DQJHoNcTyS3Z1ChvWQB/s1600/book3.gif"></div>
<div id="card4" class="ui-draggable" style="position:relative">
<img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEibD8A2kMAlh2QaMbD9edthhBWJtcmBemstfi00IHRrNPJe3oO_urpTvVG_cTOWaq7FnT3QSTceqtgCKKxK3i3_dbVcyhelkg6FtabfD_QH0o3ZwauPjtJKAp3RS4LSdlmavppocyKLa74r/s1600/book4.gif"></div>
<div id="card5" class="ui-draggable" style="position:relative">
<img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgjCj3e5G-DiA5KRRDIyDH0D5EQStZFK3mL1RgFG7huE_BsTNni0hXVHDWvph8OjXqz64zoCt34AIt1ej_IhLds_sm4q03Dry1kAoXyUCsGuu-pZ-nudMiRH1Htp_YuchBXMqBt81ufWASA/s1600/book5.gif"></div>
<div id="card6" class="ui-draggable" style="position:relative">
<img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUGaafq53HE0HQ-LqXkXmC8H6X4XU0WzNIQb2Lmm-G6YlJ4qPbm-hhJvNtBgk8rm9FxI0EkgzJONnpHmdOz0Njufkw_hbhM06nHVYbqNBbULcGq_olE2UcI7DZD2ON7doOLsWrMYeyuiKO/s1600/book6.gif"></div>
</div>
<div class="clear"> </div>
<div id="cardSlots">
<div class="ui-droppable">first</div>
<div class="ui-droppable">second</div>
<div class="ui-droppable">third</div>
<div class="ui-droppable">fourth</div>
<div class="ui-droppable">fifth</div>
<div class="ui-droppable">sixth</div>
</div>
<p> </p>
<div class="clear"> </div>
<audio id="sound_tag" src="http://www.grammar-quizzes.com/sounds/clapping.wav" preload="auto"></audio>
<div id="successMessage" style="display:none;left:10em;top:100em;width:0;height:0">
<h3>You did it! </h3>
<p> </p>
<p class="floatright">
<button onclick="location.href='wrcite2_mag.html'">Continue</button>
</p>
</div>
<div class="clear"> </div>
<p>
<button onclick="init()">Reset</button></p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</div>
<footer id="footer">
</footer>
</div>
</body>
</html>
---------------------------------------------------------------------------------------------------------------------
SIMPLE MATCHING GAME
---------------------------------------------------------------------------------------------------------------------
<html>
<head>
<style>
#TextBox {
background-color: coral;
color: blueviolet;
text-align: center;
font-weight: bold;
}
.matched {
background: green;
}
</style>
</head>
<body>
<div id="TextBox"></div>
<button id="0">A</button>
<button id="0">APPLE</button>
<button id="1">B</button>
<button id="1">BALL</button>
<script>
var firstClickedTile;
var secondClickedTile;
var buttons = document.querySelectorAll("button");
for (var i = 0; i < buttons.length; i++) {
buttons[i].addEventListener('click', matchingFunction);
}
function matchingFunction(event) {
var clickedButton = event.target;
if (!firstClickedTile) {
firstClickedTile = clickedButton;
}
else {
if (firstClickedTile !== clickedButton) {
secondClickedTile = clickedButton;
TextBox.innerHTML = "Try Again";
if (firstClickedTile.id == secondClickedTile.id) {
firstClickedTile.classList.add("matched");
secondClickedTile.classList.add("matched");
TextBox.innerHTML = "Good Job";
}
firstClickedTile = undefined;
}
}
}
</script>
</body>
</html>
--------------------------------------------------------------------------------------------------------------
Other Code
--------------------------------------------------------------------------------------------------------------
<!DOCTYPE html>
<html>
<body>
<button onclick="match(this)">Apple</button>
<button onclick="match(this)">Ball</button>
<button onclick="match(this)">Ant</button>
<button onclick="match(this)">Banana</button>
<p>Score: <span id="score">0</span></p>
<script>
let lastButton = null;
let score = 0;
function match(btn) {
if (!lastButton) {
lastButton = btn;
btn.style.background = 'yellow';
} else if (btn !== lastButton) {
if (btn.textContent[0] === lastButton.textContent[0]) {
score++;
document.getElementById('score').textContent = score;
btn.remove();
lastButton.remove();
} else {
lastButton.style.background = '';
}
lastButton = null;
}
}
</script>
</body>
</html>
--------------------------------------------------------------------------------------------------------------
Other Code
--------------------------------------------------------------------------------------------------------------
<!DOCTYPE html>
<html>
<head>
<style>
.BtnClass {
width: 100px;
height: 60px;
font-size: 20px;
margin: 10px;
}
</style>
</head>
<body>
<button class="BtnClass" onclick="buttonClicked(this)">Button 1</button>
<button class="BtnClass" onclick="buttonClicked(this)">Button 1</button>
<button class="BtnClass" onclick="buttonClicked(this)">Button 2</button>
<button class="BtnClass" onclick="buttonClicked(this)">Button 2</button>
<script>
let clicked1 = null;
function buttonClicked(button) {
// First button click
if (clicked1 === null) {
clicked1 = button;
clicked1.style.background = 'orange';
return;
}
// Ignore if same button clicked twice
if (button === clicked1) return;
// Second button clicked
button.style.background = 'orange';
if (clicked1.innerText === button.innerText) {
// Match: remove both
setTimeout(() => {
clicked1.remove();
button.remove();
clicked1 = null;
}, 300);
alert("✅ Match Found!");
} else {
// Not match: reset both
setTimeout(() => {
clicked1.style.background = '';
button.style.background = '';
clicked1 = null;
}, 500);
alert("❌ Try Again");
}
}
</script>
</body>
</html>
} else if (btn !== lastButton) {
if (btn.textContent[0] === lastButton.textContent[0]) {
score++;
document.getElementById('score').textContent = score;
btn.remove();
lastButton.remove();
} else {
lastButton.style.background = '';
}
lastButton = null;
}
}
</script>
</body>
</html>
-------------------------------------------------------------------------------------------------------------
Other Code Hide Button Immediately When Clicked
--------------------------------------------------------------------------------------------------------------
<html>
<head>
<style>
.BtnClass {
width: 100px;
height: 60px;
font-size: 20px;
margin: 10px;
}
</style>
</head>
<body>
<!-- Buttons with matching data-match values -->
<button class="BtnClass" data-match="1" onclick="buttonClicked(this)">Button 1</button>
<button class="BtnClass" data-match="1" onclick="buttonClicked(this)">Button 1</button>
<button class="BtnClass" data-match="2" onclick="buttonClicked(this)">Button 2</button>
<button class="BtnClass" data-match="2" onclick="buttonClicked(this)">Button 2</button>
<button class="BtnClass" data-match="3" onclick="buttonClicked(this)">Button 3</button>
<button class="BtnClass" data-match="3" onclick="buttonClicked(this)">Button 3</button>
<script>
let firstButton = null;
let clickedButtons = [];
function buttonClicked(button) {
// Hide button immediately when clicked
button.style.visibility = 'hidden';
clickedButtons.push(button);
// First click
if (firstButton === null) {
firstButton = button;
return;
}
// Compare match values
if (firstButton.dataset.match === button.dataset.match) {
// Remove both matching buttons permanently
firstButton.remove();
button.remove();
alert("✅ Match Found!");
} else {
// Show both buttons again
clickedButtons.forEach(btn => btn.style.visibility = 'visible');
alert("❌ Not a Match - Try Again");
}
// Reset after second click
firstButton = null;
clickedButtons = [];
}
</script>
</body>
</html>
--------------------------------------------------------------------------------------------------------------
Other Code
Hide Button Immediately When Clicked
Without Data Set Code
--------------------------------------------------------------------------------------------------------------
<!DOCTYPE html>
<html>
<head>
<style>
.BtnClass {
width:100px;
height:60px;
font-size:20px;
margin:10px;
}
</style>
</head>
<body>
<!-- Buttons with matching IDs -->
<button id="A1" class="BtnClass" onclick="buttonClicked(this)">Button 1</button>
<button id="A1" class="BtnClass" onclick="buttonClicked(this)">Button 1</button>
<button id="B1" class="BtnClass" onclick="buttonClicked(this)">Button 2</button>
<button id="B1" class="BtnClass" onclick="buttonClicked(this)">Button 2</button>
<script>
let firstButton = null;
let clickedButtons = [];
function buttonClicked(button) {
// Hide button immediately when clicked
button.style.visibility = 'hidden';
clickedButtons.push(button);
// First click
if (firstButton === null) {
firstButton = button;
return;
}
// Compare IDs
if (firstButton.id === button.id) {
firstButton.remove();
button.remove();
alert("✅ ID Match Found!");
} else {
// Show both buttons again
clickedButtons.forEach(btn => btn.style.visibility = 'visible');
alert("❌ Not a Match");
}
// Reset
firstButton = null;
clickedButtons = [];
}
</script>
</body>
</html>
--------------------------------------------------------------------------------------------------------------
Other Code
Hide Button Immediately When Clicked
Without Data Set Code
Prevent Clicked Same Button
Short Return Code Don't Need Extra Return Line
if (clicked1 === null) {
clicked1 = button;
button = clicked1
return;
}
Match Found Buttons Visible But Both
Button Disabled
/ Compare IDs
if (clicked1.id === button.id) {
clicked1.style.visibility = 'visible';
button.style.visibility = 'visible';
clicked1.disabled = true;
button.disabled = true;
alert("✅ Match Found!");
clicked1 = null;
}
--------------------------------------------------------------------------------------------------------------
<!DOCTYPE html>
<html>
<head>
<style>
.BtnClass {
width: 100px;
height: 60px;
font-size: 20px;
margin: 10px;
background: #ddd;
border: none;
border-radius: 5px;
cursor: pointer;
}
.BtnClass:hover {
background: #ccc;
}
.hidden {
visibility: hidden;
}
</style>
</head>
<body>
<!-- Buttons with matching IDs -->
<button id="A1" class="BtnClass" onclick="buttonClicked(this)">Button1</button>
<button id="A1" class="BtnClass" onclick="buttonClicked(this)">Button1</button>
<button id="B1" class="BtnClass" onclick="buttonClicked(this)">Button2</button>
<button id="B1" class="BtnClass" onclick="buttonClicked(this)">Button2</button>
<script>
let clicked1 = null;
function buttonClicked(button) {
// Hide the button immediately when clicked
button.style.visibility = 'hidden';
// First click
if (clicked1 === null) {
clicked1 = button;
return;
}
// Prevent double-clicking the same button
if (button === clicked1) return;
// Compare IDs
if (clicked1.id === button.id) {
alert("✅ Match Found!");
clicked1 = null;
} else {
// Show both buttons again if no match
clicked1.style.visibility = 'visible';
button.style.visibility = 'visible';
alert("❌ Not a Match");
clicked1 = null;
}
}
</script>
</body>
</html>
--------------------------------------------------------------------------------------------------------------
Other Code Button Code Without Null variable
--------------------------------------------------------------------------------------------------------------
<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");
for(var i=0;i<buttons.length;i++){
buttons[i].addEventListener("click",buttonClicked);
}
function buttonClicked(event){
var button=event.target;
var buttons=document.querySelectorAll("button");
for(var i=0;i<buttons.length;i++){
if(buttons[i].style.visibility==="hidden"){
if(buttons[i]===button)break;
if(buttons[i].id===button.id){
buttons[i].remove();
button.remove();
alert("✅ Match Found!");
}else{
buttons[i].style.visibility="visible";
alert("❌ Not a Match");
}
return;
}
}
button.style.visibility="hidden";
}
</script>
--------------------------------------------------------------------------------------------------------------
Other Code
--------------------------------------------------------------------------------------------------------------
<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");
for (var i = 0; i < buttons.length; i++) {
buttons[i].onclick = function() {
var buttons = document.querySelectorAll("button");
for (var i = 0; i < buttons.length; i++) {
if (buttons[i].style.visibility === "hidden") {
if (buttons[i].id === this.id) {
buttons[i].remove();
this.remove();
return alert("✅ Match Found!");
}
buttons[i].style.visibility = "visible";
return alert("❌ Not a Match");
}
}
this.style.visibility = "hidden";
};
}
</script>
--------------------------------------------------------------------------------------------------------------
Other Code
--------------------------------------------------------------------------------------------------------------
<!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");
for (var i = 0; i < buttons.length; i++) {
buttons[i].onclick = function() {
var buttons = document.querySelectorAll("button");
for (var i = 0; i < buttons.length; i++) {
if (buttons[i].style.visibility === "hidden") {
if (buttons[i].id === this.id) {
buttons[i].disabled = true; // Disable the hidden matching button
this.disabled = true; // Disable the clicked button
buttons[i].style.visibility = "visible"; // Show the hidden button
return alert("✅ Match Found!");
}
buttons[i].style.visibility = "visible";
return alert("❌ Not a Match");
}
}
this.style.visibility = "hidden";
};
}
</script>
--------------------------------------------------------------------------------------------------------------
Other Code
--------------------------------------------------------------------------------------------------------------
<!DOCTYPE html>
<html>
<head>
<style>
.BtnClass {
width: 100px;
height: 60px;
font-size: 20px;
margin: 10px;
}
</style>
</head>
<body>
<!-- Buttons with matching data-match values -->
<button id="btn1a" class="BtnClass" data-match="1" onclick="buttonClicked(this)">Button 1</button>
<button id="btn1b" class="BtnClass" data-match="1" onclick="buttonClicked(this)">Button 1</button>
<button id="btn2a" class="BtnClass" data-match="2" onclick="buttonClicked(this)">Button 2</button>
<button id="btn2b" class="BtnClass" data-match="2" onclick="buttonClicked(this)">Button 2</button>
<script>
let clicked1 = null;
function buttonClicked(button) {
// First click
if (clicked1 === null) {
clicked1 = button;
clicked1.style.background = 'orange';
return;
}
// Prevent double-clicking same button
if (button === clicked1) return;
button.style.background = 'orange';
// Compare by data-match (not text)
if (clicked1.dataset.match === button.dataset.match) {
setTimeout(() => {
clicked1.remove();
button.remove();
clicked1 = null;
}, 300);
alert("✅ ID Match Found!");
} else {
setTimeout(() => {
clicked1.style.background = '';
button.style.background = '';
clicked1 = null;
}, 500);
alert("❌ Not a Match");
}
}
</script>
</body>
</html>
--------------------------------------------------------------------------------------------------------------
Other Code
--------------------------------------------------------------------------------------------------------------
<!DOCTYPE html>
<html>
<body>
<button id="btn1" onclick="buttonClicked(event)">Button 1</button>
<button id="btn1" onclick="buttonClicked(event)">Button 1</button>
<button id="btn2" onclick="buttonClicked(event)">Button 2</button>
<button id="btn2" onclick="buttonClicked(event)">Button 2</button>
<script>
let firstClick = null;
function buttonClicked(event) {
const button = event.target;
if (firstClick) {
if (button === firstClick) return;
if (firstClick.id === button.id) {
firstClick.remove();
button.remove();
alert("✅ Match Found!");
} else {
alert("❌ Not a Match");
}
firstClick = null;
} else {
firstClick = button;
}
}
</script>
</body>
</html>
--------------------------------------------------------------------------------------------------------------
Other Code
--------------------------------------------------------------------------------------------------------------
<html>
<head>
<style>
button {
width: 120px;
height: 60px;
font-size: 18px;
margin: 10px;
cursor: pointer;
}
</style>
</head>
<body>
<!-- Buttons with same IDs for matching -->
<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 firstClick = null;
// Collect all buttons into BtnArray
var BtnArray = document.querySelectorAll("button");
// Loop through all buttons and assign click handler
for (var i = 0; i < BtnArray.length; i++) {
BtnArray[i].onclick = function(event) {
var button = event.target;
if (firstClick) {
if (button === firstClick) return;
if (firstClick.id === button.id) {
firstClick.remove();
button.remove();
alert("✅ Match Found!");
} else {
alert("❌ Not a Match");
}
firstClick = null; // Reset after second click
} else {
firstClick = button; // Store first button
}
};
}
</script>
</body>
-----------------------------------------------------------------------------------------------
Other Code
----------------------------------------------------------------------------------------------------
<html>
<body>
<!-- Big buttons -->
<button id="A" onclick="handleClick(event)" style="width:100px;height:60px;font-size:20px;margin:10px;">A</button>
<button id="A" onclick="handleClick(event)" style="width:100px;height:60px;font-size:20px;margin:10px;">A</button>
<button id="B" onclick="handleClick(event)" style="width:100px;height:60px;font-size:20px;margin:10px;">B</button>
<button id="B" onclick="handleClick(event)" style="width:100px;height:60px;font-size:20px;margin:10px;">B</button>
<p id="result" style="font-size:20px;"></p>
<script>
let clickedId1 = null;
let result = document.querySelector("#result");
function handleClick(event) {
if (event.target === clickedId1) return;
if (clickedId1 == null) {
clickedId1 = event.target;
clickedId1.style.background = "yellow";
result.textContent = "";
} else {
if (clickedId1.id === event.target.id) {
result.textContent = "Match!";
clickedId1.disabled = true;
event.target.disabled = true;
} else {
result.textContent = "Try Again!";
}
// Reset background
clickedId1.style.background = "";
event.target.style.background = "";
// Clear selection
clickedId1 = null;
}
}
</script>
</body>
</html>
</html>
-------------------------------------------------------------------------------------------------------
Other Code
---------------------------------------------------------------------------------------------------
0 comments:
Post a Comment