Reputation: 1
$(".squares").on('mouseover', function(){
$(this).css('background-color', getRandomColor());
getRandomColor() is a function which returns a random color function when a square in a grid is mouseovered.
I want to be able to go over once and the color changes, but if i go over a second time the color will stay the same. Right now the color just repeatedly changes after every mouseover. I know its probably going to require an if statement. I just don't know how to set the statement equal to a back-ground color.
Upvotes: 0
Views: 57
Reputation: 8868
You can use unbind()
event. This would unbind the mouseover
event once its already invoked.
$(".squares").on('mouseover', function(){
$(this).css('background-color', getRandomColor());
$(this).unbind('mouseover');
});
http://jsfiddle.net/j6um1sq2/17/
Upvotes: 0
Reputation: 988
Use .one()
$(".squares").one('mouseover', function() {
$(this).css('background-color', getRandomColor());
});
Upvotes: 2
Reputation: 4928
Could you add a 'data-' attribute, so you know if it has already been changed?
Upvotes: 0