Closed ghost closed 9 years ago
I'd do something like this:
var oldAnswerGlobal = null;
$(".card").on("click", function(){
var answer = $(this).val();
if(oldAnswerGlobal !== null){
if(answer == oldAnswerGlobal){
alert("Correct");
}else{
alert("Nope");
}
oldAnswerGlobal = null;
}
});
I see the idea and worked it into my code. Expected the answer and oldAnswerNull vars to update with the two clicks and compare, but this is not happening. Grid is flipping cards, answer is not defined.
var oldAnswerGlobal = null
//card clickEvent
$('.card').on('click', flipCard)
function flipCard(evt){
console.log('card clicked')
$(this).toggleClass('card')
var answer = $(this).val()
if(oldAnswerGlobal !== null){
if(answer == oldAnswerGlobal){
alert("Nice match!")
}else{
alert("Not a match, try again.")
}
oldAnswerGlobal=null
//add toggle from white to color for indiv cards
$(this).toggleClass('card')
}
}
Backtics, not commas!
.val()
only works on <input />
elements. You probably want .text()
or something.
wrong side of the keyboard!
i'm working my way through various selectors, and .attr('class') makes the most sense but does not define the variable.
var answer = $(this).attr('class')
Is this
what you expect it to be? Find out with console.log(this)
. Remember: this
is always what had the event listener attached to it.
yes: it's returning, for example, <div class="blue"></div>
, which is what i want to use to compare cards
Instead of $(this).attr("class")
, can you try this.className
? See what that is.
I'm working on the comparison part of concentration and a bit uncertain how to tackle it.
i want to compare the classes of two clicked cards, and if they are equal, return a match notification. if they are unequal, i want to return the cards to their initial states.
i think the first step is to store the click results in two variables, but have been spinning my wheels.
Have tried to chunk the process with pseudocode, but at a standstill.