Reputation: 259
I'm making a very simple minigame which idea is to pick the proper color of the figure you've seen for about a second or two by clicking one of the four divs with the id's circle1, circle2 etc. with the specific colors:
var odpowiedz_kolor = $('<div class="label id="gra">Jaki kolor miał następujący kształt?</br></br><ul class="inline">'+
'<div class="odpowiedz_pojemnik" id="' + wylosowane[losowa_z_wylosowanych_figur].figura + '"> </div></br></br>'+
'<ul class="inline">'+
'<li><div class="kolo" id="kolo1"> </div></li>'+
'<li><div class="kolo" id="kolo2"> </div></li>'+
'<li><div class="kolo" id="kolo3"> </div></li>'+
'<li><div class="kolo" id="kolo4"> </div></li>'+
'</ul></div>');
I've cut out the unnecessary code...
$('#kolo1').css('background-color', wylosowane[0].kolor);
$('#kolo2').css('background-color', wylosowane[1].kolor);
$('#kolo3').css('background-color', wylosowane[2].kolor);
$('#kolo4').css('background-color', wylosowane[3].kolor);
$(".kolo").on('click', function(){
var color = $('.kolo').find('#kolo').css('background-color');
I've set the colors as seen above. Now's the question how to retrieve the according colors 'cause I'm stuck on the click handler. I'd appreciate even the ugly but working solutions.
The scenario is:
Thank you in advance.
Upvotes: 2
Views: 107
Reputation: 8275
You probably want to know the color of the figure that you just clicked, so I suggest you made this :
$(".kolo").on('click', function(){
var color = $(this).css('background-color');
}
JsFiddle : http://jsfiddle.net/F3HB5/
EDIT :
Based on your code, I have updated your jsFiddle to this one : http://jsfiddle.net/8LFR7/3/.
There were 2 main errors :
#
before the id -> $("#" + wylosowane[losowa_z_wylosowanych_figur].figura).css('background-color')
.setTimeout
block : var targetColor = $("#" + wylosowane[losowa_z_wylosowanych_figur].figura).css('background-color');
Upvotes: 1
Reputation: 394
Firstly a friendly advice, whichever native language you belong to, please use English for your code.
Now your solution: Well you need to match the background-color for the clicked circle with the triangle, so you can do the following to achieve this:
$(".kolo").on('click', function(event){
var id = event.target.id; // For the id, but that does not matter, you can get it using the class also or this
if($(id).css('background-color') == $('.odpowiedz_pojemnik').css('background-color')){
alert('Good!');
}
else{
alert('Bad!');
}
}
Upvotes: 0