Compare button pressed with another in javascript

0

first of all I am new in javascript and in this forum. I appreciate if you can help me in the following: I have a website with 9 buttons and 9 images. The idea is to press a button, a sound is played (I already have it working). What I need is that when I weigh on an image, it must correspond with the sound. Example if a button is pressed and the sound of the chick is heard, the user if he selects the chick image, will show him a message, if he selects another image, he should show him an error message. For clarity, this is the image of the web:
Thank you very much for the ideas you can give me.

    
asked by Jerry 12.10.2018 в 21:01
source

1 answer

1

Look, what I can think of is something like that.

First add a data attribute to the buttons:

data-sonido="pollito"

When you press a button, save the data attribute in a variable, something like this:

$('.botones').click(function(){
    var btn_sonido = $(this).data('sonido');
})

Then I would give each image another data attribute:

data-imagen="pollito"

And create the event for each time you click on an image to get the value of the data attribute:

$('.imagenes').click(function(){
    var img_animalito = $(this).data(imagen);
});

And once you have done this, compare the variables

if(img_animalito == btn_sonido){
    alert("Bien hecho!");
}else{
    alert("Te has equivocado de animalito, vuelve a intentarlo.");
}

It's what I think of, I hope it works for you.

    
answered by 12.10.2018 / 22:08
source