Jim
Jim

Reputation: 9234

Change div width in percentage dynamically

I have a div

 <div id="chart" style="position:relative; width: 100%; float:left; height: 220px;">
</div>

And I need to change width dynamically. I found solution like:

var elem = document.getElementById('chart');

   elem.style.width = 70 + "%";

But that not work's. Where I do wrong ? Thanks in advance...

Upvotes: 2

Views: 34993

Answers (2)

Selvakumar Ponnusamy
Selvakumar Ponnusamy

Reputation: 5563

It works well for me.

Browsers Used: IE8 and FF3.

Could you verify once other wise you may try this

elem.style.width =  "70%";

This is the code I tested

<body>
    <div id="chart" style="position:relative; width: 100%; float:left; height:  220px;border:2px Solid #FF0000">
    </div>
</body>

var elem = document.getElementById('chart');
elem.style.width =  70+ "%";

Upvotes: 1

Niels
Niels

Reputation: 49949

Try this fiddle: http://jsfiddle.net/TrrCh/

I think you are trying to run the Javascript before the HTML is loaded. Bind it to the document.ready and it should be working

window.onload=function(){
    var elem = document.getElementById('chart');
    elem.style.width = 70 + "%";

}

Upvotes: 8

Related Questions