views:

431

answers:

7

I have a div defined with a style like this:

<div id="div1" style="width:600;height:600;border:solid 1px"></div>

How can I change the height of the div by Javascript with a function?

<script type="text/javascript">
function changeHeight(height) {
   var node = dojo.byID("div1");
   // change height?
}
<a href="javascript:changeHeight(300);">Change height to 300</a>
+2  A: 

Here is how it might look with jQuery:

<div id="div1" style="width:600;height:600;border:solid 1px"></div>
<a href="#">Change height to 300</a>

<script type="text/javascript">
    $(function() {
        $('a').click(function() {
            $('#div1').css('height', '400px');
            return false;
        });
    });
</script>
Koistya Navin
+8  A: 
<script type="text/javascript">
function changeHeight(height)
{
   document.getElementById("div1").style.height = height + "px";
}
</script>
Sebastian Celis
+5  A: 
document.getElementById("div1").style.height = height + "px";
ChristopheD
+2  A: 
var d = document.getElementById("div1");
d.style.height = "300px";
Seb
+1  A: 

Just replace your comment with:

node.style.height = height;

Oh, not sure if just passing 300 to your function will make it work, perhaps you'll have to pass "300px" like suggested in the other posts...

Sam
+5  A: 

Judging by his example code he is using the dojo framework. Changing height in dojo would be done with something similiar to the following:

dojo.style("div1", "height", 300);

http://api.dojotoolkit.org/jsdoc/dojo/1.2/dojo.style

Rick Hochstetler
+1 for recognizing that the OP may want to keep using his framework.
Peter LaComb Jr.
I wish I had more upvotes for today.
strager
A: 

In dojo, you would do it like this:

dojo.style("div1", "height", "300px");

Having the units on the height is important, as mentioned in the docs.

Ben Lowery