Let's start by providing the id image:
<img id="image" alt="Web_ileana-d-cruz" class="" src="https://photochute-dev.s3.amazonaws.com/uploads/event_image/image/108/web_ileana-d-cruz.jpg?c=1435318866">
Now just take both the image and the id rotate button and add an event listener to the rotate button:
var image = document.getElementById("image"); var rotateButton = document.getElementById("rotate_image_button"); rotateButton.addEventListener("click", function(e) { e.preventDefault(); if(image.className === "rotate-90") { image.className = ""; } else { image.className = "rotate-90"; } return false; });
What is it. Basically, when you click, you check if the image has the class rotated or not, and then just adds / removes it.
This is even easier if you use jquery. If the image can have no more than one class, you will need to use:
if((image.className+" ").indexOf("rotate-90 ") !== -1) { image.className = (image.className+" ").replace("rotate-90 ", ""); } else { if(image.className) { image.className += " rotate-90"; } else { image.className = "rotate-90"; } }
JSFiddle: http://jsfiddle.net/wo6mos9r/3/
Sebastian Nette Jun 26 '15 at 12:26 2015-06-26 12:26
source share