4

please help me to build following logic, here is code:

<button onclick="zoomin()">Zoom In</button>

function zoomin(){
   var myImg = document.getElementById("img");
   var i = 0;
   i++;
   myImg.style.transform = "scale(1."+ i +")";
}

but, because of i = 0 it works only one time. please tell me solution.

1
  • 2
    the problem is that every time you run the function, the var i is being set back to 0. Try moving the var i declaration outside of the function. Commented Mar 5, 2016 at 9:45

2 Answers 2

5

Move the variable i out of the function, it needs to be initialised only once.

var myImg = document.getElementById("img");
    var i = 0;
    function zoomin(){


       i++;
       myImg.style.transform = "scale(1."+ i +")";
    }
Sign up to request clarification or add additional context in comments.

Comments

2
var i = 0;
var myImg = document.getElementById("img");
function zoomin(){
   i++;
   myImg.style.transform = "scale(1."+ i +")";
}

declare i=0; outside function will solve you probleem

2 Comments

Also the handle to myImg should be moved outside the function as well.
why declare the same variable over and over and waste computational power.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.