user1760770
user1760770

Reputation: 395

Resize dynamically created div around its content

I create div like this:

$(ndiv).addClass("table").html(table.Table.Name).appendTo($("#tables"));

I'd like to be able to set its width to exactly fit table.Table.Name string. Is something like that possible? Thanks in advance.

Upvotes: 0

Views: 102

Answers (2)

Olaf Dietsche
Olaf Dietsche

Reputation: 74018

An easy solution would be to wrap the string in a span element and pick the width from that

$(ndiv).addClass("table").html('<span>' + table.Table.Name + '</span>').appendTo($("#tables"));
var w = $('span', ndiv).width()
$(ndiv).width(w);

Upvotes: 2

Mihail
Mihail

Reputation: 1479

If table.Table.Name string is table tag with content, you can try this code:

var $block = $(ndiv).addClass("table").html(table.Table.Name).appendTo($("#tables"));
$block.width($block.children("table").width());

Or you can try simple way, without js, if you page-proofs than allow. Set CSS:

.table{display: inline-block;}

Upvotes: 1

Related Questions