Reputation: 395
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
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
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