Reputation: 120
I am trying to setup an element that will rotate 180 degrees when clicked and then back again when clicked again. The code I am using is as follows. Is obviously not right and feels like I need to use a for loop.
$('#hodgepodge').click(function(){
var i = 0;
if (i < 1) {
$('#hodgepodge').rotate(180);
var i = 1;
} else {
$('#hodgepodge').rotate(0);
var i = 0;
}
});
Upvotes: 1
Views: 73
Reputation: 318312
You can use jQuery's data()
to keep track of the state, and then just toggle it :
$('#hodgepodge').on('click', function(){
$(this).rotate( !$(this).data('state') ? 180 : 0 );
$(this).data('state', !$(this).data('state'));
});
Upvotes: 1
Reputation: 28753
Try to initialize before click
event like
var i = 0;
$('#hodgepodge').click(function(){
if (i < 1) {
$('#hodgepodge').rotate(180);
i = 1;
} else {
$('#hodgepodge').rotate(0);
i = 0;
}
});
Upvotes: 0