I have a jQuery code that moves a certain div element on right as I click it, i'd like to move it back to its original position when I click back on it.
$(document).ready(function() {
$("#wel1").click(function() {
$("#sidemenu").animate({left: "80px"});
});
});
Give me a simple example of how to achieve it?
You can simplify this by adding an attribute to store the current state of the div. Implemented at : http://jsfiddle.net/mwtrsw9p/
HTML:
CSS :
I think this way is best:
css:
js:
You could do something like this:
var toggle = 1;
You have a variable 'toggle' with a value of 1, when you click it will check if 'toggle' equals 1, if it does it will move to the left, and then set 'toggle' to 0, because of this the first if will result false if you click a second time, and it will use the if statement and move the object to the right, after which it will set the value of 'toggle' to 1 which will result in your desired behaviour.
One possible way to achieve it is to store the state in a variable and check its state on click.
Please be aware that this solution has several issues:
You need a status to check whether it has been clicked or not, like below:
Check out my fiddle..