dave
dave

Reputation: 475

How to change the width and height of a DIV using variables in javascript?

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.

Upvotes: 1

Views: 2140

Answers (4)

AshBringer
AshBringer

Reputation: 2673

//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/

Upvotes: 0

jrath
jrath

Reputation: 990

Use like this.

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

Upvotes: 0

Execute.H
Execute.H

Reputation: 118

Return the width property:

object.style.width 

Set the width property:

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

same for height

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

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';

Upvotes: 2

Related Questions