Hello. I'm doing a little college job where a kid needs to click on the wrong picture in a group with 3 other pictures. When clicking on one of the images, it receives feedback if that is the right message or not, a button appears to display the result and a button to go to a next group of images, which is nothing more than a hidden div that happens to be visible, while the current becomes invisible.
I need to stop the onclick of the images, so that I can only click one at a time. I managed to make this work, but I could not get the onclick back on the next div.
I used it this way:
$("img").prop('onclick', null).off('click');
I tried to use on () later, but I could not. Now I'm using off (), but I can not even disable it. Here is the code:
function imagemCerta(imagem){
var nome = $(imagem).attr('name');
switch($(imagem).closest("div").attr('id')){
case "comidas":
if( nome == "brocolis") {
total = total + 1;
console.log("Total", total);
}
$("img").off('click');
$("button[name='resultado']").show();
$("button[name='resultado']").click(function(){
$("img[name='hamburguer']").attr("src", "./imagens/hamburguer.png");
$("img[name='brocolis']").attr("src", "./imagens/check.png");
$("img[name='pizza']").attr("src", "./imagens/pizza.png");
$("img[name='hotdog']").attr("src", "./imagens/hotdog.png");
$("button[name='proximo']").show();
$("button[name='resultado']").hide();
$("button[name='proximo']").click(function(){
$("div[id='comidas']").hide();
$("div[id='animais']").show();
$("img").on(imagemCerta);
});
});
break;
case "animais":
if(nome == "sanduba"){
total = total + 1;
}
$("img").off('click');
$("button[name='resultado2']").show();
$("button[name='resultado2']").click(function(){
$("img[name='cachorro']").attr("src", "./imagens/cachorro.png");
$("img[name='sanduba']").attr("src", "./imagens/check.png");
$("img[name='leao']").attr("src", "./imagens/leao.png");
$("img[name='burro']").attr("src", "./imagens/burro.png");
$("button[name='proximo2']").show();
$("button[name='resultado2']").hide();
$("button[name='proximo2']").click(function(){
$("div[id='animais']").hide();
$("div[id='transportes']").show();
$("img").on(imagemCerta);
});
});
break;
case "transportes":
if(nome == "robo"){
total = total + 1;
}
$("img").off('click');
$("button[name='resultado3']").show();
$("button[name='resultado3']").click(function(){
$("img[name='trem']").attr("src", "./imagens/trem.png");
$("img[name='robo']").attr("src", "./imagens/check.png");
$("img[name='caminhao']").attr("src", "./imagens/caminhao.png");
$("img[name='aviao']").attr("src", "./imagens/aviao.png");
$("button[name='proximo3']").show();
$("button[name='resultado3']").hide();
$("button[name='proximo3']").click(function(){
$("div[id='transportes']").hide();
$("div[id='final']").show();
$("img").on(imagemcerta);
});
});
break;
}
return;
}
My html file:
<div id="comidas">
<center>
<img name="hamburguer" src="./imagens/hamburguer.png" onclick="this.src='./imagens/close.png'; imagemCerta(this)">
<img name="pizza" src="./imagens/pizza.png" onclick="this.src='./imagens/close.png'; imagemCerta(this)">
<img name="brocolis" src="./imagens/brocolis.png" onclick="this.src='./imagens/check.png'; imagemCerta(this)">
<img name="hotdog" src="./imagens/hotdog.png" onclick="this.src='./imagens/close.png'; imagemCerta(this)">
<br>
<button name="resultado" style="display: none" class="btn btn-danger">Resultado</button>
<button name="proximo" style="display: none" class="btn btn-primary">Proximo >>> </button>
</center>
</div>