I'm writing a jQuery plugin and I want to wrap a textarea
element (whose width could be defined in any unit) inside a div
element and apply the original element's width and unit to the new parent. The pixel width is not useful, because I want the whole thing to be responsive.
<style>
#target {
width: 90%; /* 10rem, 80vw etc */
}
</style>
<script>
$(function(){
let container=$('<div>');
container.width(???); // make container's width whatever width and unit the textarea was in css - in this case 90%
$('#target').wrap(container).css('width','100%');
});
</script>
<textarea id="target"></textarea>
Expected result:
<div style="width: 90%"> <!-- or whatever width and unit was in the css -->
<textarea id="target" style="width: 100%"></textarea>
</div>
Obviously it would be easy to get the exact pixel value with outerWidth()
or window.getComputedStyle
, but that wouldn't be responsive.
Edit: Looks like this has been answered here already. It's possible, but only in Blink browsers for the time being.