Zack Macomber
Zack Macomber

Reputation: 6905

Make JQuery UI Dialog automatically grow HEIGHT to fit its contents (width remains static)

Having looked into Make JQuery UI Dialog automatically grow or shrink to fit its contents, I am using the height: "auto" option when building a jQuery modal dialog box:

$( "#dialog-message" ).dialog({
    autoOpen: false,
    width: "400",
    height: "auto",
    show: "slide",
    modal: true,
    buttons: {
        Ok: function() {
            $( this ).dialog( "close" );
        }
    }
});

However, the height isn't "growing" to fit all of the contents. I'm still seeing a vertical scrollbar as in this image:

jQuery Modal Dialog Image

Is there a way right in the definition code I listed to ensure that the height grows enough so that a vertical scrollbar doesn't show? Or, do I need to do this programmatically before opening the dialog box?

Edit 1
Not sure why, but Chrome is displaying this fine but IE 8 isn't. I need it to specifically work in IE 8 so I believe I'm just going to put a bottom margin on the text.

Upvotes: 14

Views: 22452

Answers (4)

Enfantcool
Enfantcool

Reputation: 221

This is 4 years Late but i have the same problem.

Wrote code that fixed it

Put a unique class on your dialog:

dialogClass:"someClassName"

$(".someClassName").resize(function () {
    var totalHeight = 0;
    var children = $(".someClassName").children(); //get all divs inside the dialog
    for (var i = 0; i < children.length; i++) {
        if ($(children[i]).innerWidth() > 15) {
            var childrenHeight = children[i].scrollHeight;
            totalHeight += childrenHeight;//make sure your dialog will be the correct height
        }
    }
    $("#idOfContentWithWrongHeight").innerHeight($("#idOfContentWithWrongHeight")[0].scrollHeight);//put the content at the height it should appear
    $(".someClassName").height(totalHeight);//update the height of the dialog
});

Upvotes: 1

dinesh_malhotra
dinesh_malhotra

Reputation: 1877

This all I have done for the dialog box to grow automatically

$("#edit_dependent").dialog({

  autoOpen:false,

  modal:true,

  width:800,

  position:["center",20],

  minHeight:"auto"

});

Upvotes: 9

Daniel Bidulock
Daniel Bidulock

Reputation: 2354

That is very strange... I'm not sure how helpful this will be, but I did manage to get the auto-height to work with the following code:

<html>
<head>
<link href="jqueryUI/css/ui-lightness/jquery-ui-1.8.19.custom.css" 
    rel="stylesheet" type="text/css">
<script src="jquery-1.7.1.js"></script>
<script src="jqueryUI/js/jquery-ui-1.8.19.custom.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $("#dialog").dialog({
    autoOpen: false,
    width: "400",
    height: "auto",
    show: "slide",
    modal: true,
    buttons: {
        Ok: function() {
            $( this ).dialog( "close" );
        }
    }
    });
    $("#dialog").dialog('open');
});
</script>

</head>
<body>
<div id="dialog">
1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />
</div>
</body>
</html>

It's basically using the same structure you've already established.

Upvotes: 2

coder
coder

Reputation: 13248

Here is the sample demo

 $( "#dialog-message" ).dialog({
        modal: true,
        height: "auto",
                buttons: {
                    Ok: function() {
                        $( this ).dialog( "close" );
                    }
                }

            });
            setTimeout(function() {
            $('#inside').append("Hello!<br>");
            setTimeout(arguments.callee, 1000);
  },1000);​

Upvotes: 0

Related Questions