javascript - Height change to auto works for all elements -


this code changes height of div auto when click "more+" button.

how can run function div want?

currently works divs when click on "viwviewmori" button.

another issue when click "viwviewmori" button second time, want change text "... less -" "more" again.

<!doctype html>     <html>     <head>     <meta charset="utf-8">     <title>untitled document</title>     </head>     <body>     <div class="hidi">     <div class="uner-sch">     <input type="checkbox"/>     first     </div>     </div>     <div class="viwviewmori"> ... more +</div>     <div class="hidi">     <div class="uner-sch">     <input type="checkbox"/>     second     </div>     </div>     <div class="viwviewmori"> ... more +</div>       <script>     $(".viwviewmori").click(function(){     var el = $('.hidi'),         curheight = el.height(),         autoheight = el.css('height', 'auto').height();     el.height(curheight).animate({height: autoheight}, 1000),               $(this).text('... less -');     });     </script>      </body>     </html> 

the click handler function takes argument event object.

so, may use event.target.

for text changed can use:

 el.text( (el.text().indexof('more')> -1) ? '... less -' : '... more +'); 

$(function () {    $(".viwviewmori").click(function (e) {      var el = $(e.target),          hidi = el.prev('div.hidi'),          curheight = hidi.height(),          autoheight = hidi.css('height', 'auto').height() * ((el.text().indexof('more') > -1) ? 3 : 1);      hidi.height(curheight).animate({height: autoheight}, 1000),        el.text(((el.text().indexof('more') > -1) ? el.text().replace(/more \+$/g, 'less -') : el.text().replace(/less \-$/g, 'more +')));    });  });
.viwviewmori {    width: 30%;    border:1px solid black;  }
<script src="https://code.jquery.com/jquery-1.12.1.min.js"></script>     <div class="hidi">      <div class="uner-sch">          <input type="checkbox"/>          first      </div>  </div>  <div class="viwviewmori"> ... more +</div>  <div class="hidi">      <div class="uner-sch">          <input type="checkbox"/>          second      </div>  </div>  <div class="viwviewmori"> ... more +</div>


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -