Zarthus Zarthus - 1 year ago 100
Javascript Question

How to determine the height and width of HTML user input and tell gridster to fit accordingly?

I'm currently using Gridster.js ( in combination with CKEditor.

Once the user saves their content with CKEditor, this content is put into the widget. However the widgets do not automatically resize themselves to fit the content, and while the user is able to resize it themselves, it would be more convienient for the userbase to have it be done for them the moment they press save.

I have tried a few things, but none to any avail. I'm having trouble getting the size of the current content, and then resizing the gridster respectively.

In my code, I have two values to work with. the gridster element (

), and the value that will be put into it (
). I have to determine the height of the
. Once this is done successfully, I will be able to determine if my code for getting the x and y values work.

My current code looks like this:

// Initialization of the gridster element.
// The base dimensions are relevant to understand how we
// calculate the multipliers, later on.
gridster = $(".gridster > ul").gridster({
widget_margins: [10, 10],
widget_base_dimensions: [100, 50],
draggable: {
handle: 'header'
resize: {
enabled: true,
max_size: [20, 10],
min_size: [2, 1]

And (the relevant bits of) my JavaScript class that handles saving and resizing:

// Saves the content from CKEditor to the gridster widget = function (data) {
var lastContents = this.default_html + data + '</div>';

this.resize_widget(this.editor, lastContents);

/* @TODO: resize_widget function */
// if the new content from ckeditor is larger than the
// original size of the widget, we need to make it larger.
this.resize_widget = function(widgetId, contents) {

var element = $('<div>')
.fake-div-gs-w-resize {
position: absolute;
display: none;
height: auto;
width: auto;
white-space: nowrap;
.css('display', 'block')

var widget = $(widgetId);

var elementWidth = $(element).width(), // I am expecting this to return the width of the content, but it returns 0.
elementHeight = $(element).height(), // As you might imagine, this also returns 0.
width = widget.width(),
height = widget.height();

$(element).css('display', 'none');

console.log(widgetId, widget, width, height, elementWidth, elementHeight);

// this code never gets past here, because element{Height,Width} returns 0.
if (elementHeight > height || elementWidth > width) {

var width_multiplier = 100, // data-x = 1 === width_multiplier px
height_multiplier = 50; // from "widget_base_dimensions: [100, 50],"

var x = Math.round(width / width_multiplier),
y = Math.round(height / height_multiplier),
eX = Math.ceil(elementWidth / width_multiplier),
eY = Math.ceil(elementHeight / height_multiplier);

console.log("setting to x:" + eX + ", y:" + eY + " with width:" + width + ", height:" + height);

if (eX >= x && eY >= y)
gridster.resize_widget(widget, eX, eY);


Whilst I am not completely confident in my logic for determining the sizes; the main focus of this question is with determining the size of the HTML contents, as what I gathered from other SO posts did not seem to help in my case.

Answer Source

You need to actually add the element to the DOM for the width() and height() functions to work. In your example, the element is not added to the document.

See this JS Fiddle as an example

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download