Search code examples
javascripthtmlcssfunctionsetinterval

The image doesn't get more wide


I was trying to make a mini image editor with a width and height function. The image should keep getting more wide after button is clicked. I tried debugging with printing the width in the console and it did but the image didn't get wider. I also tried many other variations and all of them didn't work.

so, here is my code:

function go1(){
setInterval( function() {document.getElementById("testimage").style.width + 10}, 250)
}


Solution

  • Your first problem is that while you are calculating a new width, you are aren't setting the new width in your code.

    The second problem is that IMG tags don't use the width style. Instead, they have a width attribute

    function go1(){
      setInterval( function() {
        var im = document.getElementById("testimage");
        var newWidth = (im.getAttribute("width") ? im.getAttribute("width") : 0) + 10;
        im.setAttribute("width", newWidth);
      }, 250)
    }