1

I am trying to change the width and height of a DIV dynamically using variables in javascript. My code below is not working for me.

 div_canvas.setAttribute('style', "width: '+w_resized+'px");
 div_canvas.setAttribute('style', "height: '+h_resized+'px");

Where w_resized and h_resized are variables.

3
  • 1
    div_canvas.style.width = w_resized + 'px'; Commented Jul 28, 2015 at 10:52
  • 1
    div_canvas.style.height = h_resized + 'px'; Commented Jul 28, 2015 at 10:53
  • @ArunPJohny thanks man! Commented Jul 28, 2015 at 10:57

4 Answers 4

2

Using setAttribute will override the previously provided values.... you can use the style property like

div_canvas.style.width = w_resized + 'px';
div_canvas.style.height = h_resized + 'px';
Sign up to request clarification or add additional context in comments.

Comments

0

Return the width property:

object.style.width 

Set the width property:

object.style.width="auto|length|%|initial|inherit" 

same for height

Comments

0

Use like this.

 div_canvas.style.width = w_resized+'px';
 div_canvas.style.height = h_resized+'px';

Comments

0
//HEIGHT
function upBig(id,ud) {
       var div=document.getElementById(id);
       var h=parseInt(div.style.height)+ud;
       if (h>=1){
          div.style.height = h + "px";
       }
}

//WIDTH

function upBig2(id,ud) {
       var div=document.getElementById(id);
       var h=parseInt(div.style.width)+ud;
       if (h>=1){
          div.style.width = h + "px";
       }
}

http://jsfiddle.net/1cotj5o5/65/

Comments

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.