javascript - Adjust parent div height according to child absolute div -


well question sequel of previous question in answer partly.

i need calculate content div height , apply wrapper div on basis of textarea auto expanding.

now expanding main div working wrapper div height not reducing accordingly when remove content of textarea/content div. because default calculating height of content div not resetting it. below lines that

var divheight = $('.content').height();  $('.wrapper').css('min-height', divheight+40+'px'); 

how make auto resize height of wrapper div.

demo here

  • one solution not use position:absolute element.

so main problem .content set position:absolute.
due absolute positioning need retrieve changed height dynamically in order apply parent div.

  • the other solution:

jsfiddle demo

var $content  = $('.content'), // cache selectors better performance      $wrapper  = $('.wrapper'),      $textarea = $("textarea");    function setcontentheight() {      $wrapper.css('min-height', $content.height() + 40 );  }    $textarea.on("input change", function() {       $(this).height(1).height( this.scrollheight ); // manipulate textarea height      setcontentheight();  }).change();                                       // trigger    // when need reflect .content height?  setcontentheight();                                //  $(window).on("resize", setcontentheight);          // , on win resize
.wrapper{    background:#f90;    position:relative;  }  .content{    background:#dcffc8;    margin:10px;    position:absolute;    top:0;    left:0;    right:0;    padding:10px;  }  textarea{    outline:none;    resize: horizontal;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div class="wrapper">    <div class="content">      <p>        lorem ipsum...      </p>      <div>        <textarea></textarea>      </div>    </div>  </div>


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -