I want to make div go left the first time i click on it, back to its original position the second time, left again the third etc..
So why isn't this working: /
var checker = new Boolean();
checker = true;
if (checker = true){
$("#div").click(function () {
$(this).animate({left: "10%"},400);
checker = false;
});
}
if (checker != true){
$("#div").click(function () {
$(this).animate({left: "30%"},400);
checker = true;
});
}
I want to make div go left the first time i click on it, back to its original position the second time, left again the third etc..
So why isn't this working: http://jsfiddle/kS7KE/2/
var checker = new Boolean();
checker = true;
if (checker = true){
$("#div").click(function () {
$(this).animate({left: "10%"},400);
checker = false;
});
}
if (checker != true){
$("#div").click(function () {
$(this).animate({left: "30%"},400);
checker = true;
});
}
Share
Improve this question
asked Apr 12, 2013 at 22:04
KoiskiKoiski
5682 gold badges8 silver badges23 bronze badges
8 Answers
Reset to default 3Why so much code ? :)
var checker = true;
$("#div").click(function () {
targetLeft = checker ? "10%" : "30%";
$(this).animate({left: targetLeft},400);
checker ? checker = false : checker = true;
});
http://jsfiddle/kS7KE/5/
A few issues:
if (checker = true)
You are using an assignment (
=
) rather than a check for equality (==
or===
).Your check happens at bind time
You want to check
checker
in your click event.
Soemthing like this will do the trick:
var checker = new Boolean();
checker = true;
$("#div").click(function () {
var left = checker ? "10%" : "30%";
$(this).animate({left: left},400);
checker = !checker;
});
http://jsfiddle/lbstr/L4TN3/
First, don't use new Boolean()
. Just initialize it like var checker = true;
Second, you're using an assignment operator in your first conditional (checker = true
).
Third, you're not executing the conditional on each click.
Fourth, your code is pretty convoluted. Here's a sample:
var checker = true;
$('#div').click(function() {
$(this).animate({
left: checker ? "10%" : "30%"
});
checker = !checker;
});
You should not attach multiple event handlers to the same object which conflict with each other like you did. Here is a bit clearer and simpler version which does the same thing your code is supposed to do. As you can see it makes use of only one event handler.
var checker = true;
$('#div').click(function(){
if(checker){
$(this).animate({left: "10%"},400);
checker = false;
} else {
$(this).animate({left: "30%"},400);
checker = true;
}
});
Additionally you perform an assignment in your first if statement (single =
) instead of using ==
to pare your operands.
You need to assign the click event once, and do the logic in it : http://jsfiddle/kS7KE/2/
var checker = new Boolean();
checker = true;
$("#div").click(function () {
if (checker == true){
$(this).animate({left: "10%"},400);
checker = false;
}
else{
$(this).animate({left: "30%"},400);
checker = true;
}
});
Use this
var checker = new Boolean();
checker = true;
$("#div").click(function () {
if (checker == true){
$(this).animate({left: "10%"},400);
checker = false;
}
else
{
$(this).animate({left: "30%"},400);
checker = true;
}
});
var checker = true;
$("#div").on('click', function () {
if (checker == true) {
checker = false;
$(this).animate({
left: "10%"
}, 400);
} else {
$(this).animate({
left: "30%"
}, 400);
checker = true;
}
});
Demo
Try this:
var checker = true;
$("#div").on('click', function () {
var left = checker ? '10%' : '30%';
$(this).animate({left: left},400);
checker = !checker
});