简体   繁体   中英

How to Set the Height of a DIV Element Using jQuery

I want to achieve the following

1) On click of.step2-opt

2).quiz__drawer div should get the height of.step2info

3) HTML output should be

<div class="quiz__drawer" style="height:height of .step2info div">

4) Using this jQuery code

$('body').on("click", '.step2-opt', function () {
  $(".quiz__drawer").height(function(){
    $(".step2info").outerHeight();
  });
});

Please help me to fix the jquery code.

As you can see this function equalize height after click on first div:

 $.fn.equalizeHeights = function(){ return this.height( Math.max.apply(this, $(this).map(function(i,e){ return $(e).height() }).get() ) ) } function test(){ $('.step2info, .quiz__drawer').equalizeHeights(); }
 <script src="https://code.jquery.com/jquery-3.5.1.js" integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" crossorigin="anonymous"></script> <body> <div class="step2info" onclick="test()">dkdpodkds<br>soskdsopkdspodskpskpkpk<br>sopkspokdspskposkspksapk<br></div> <div class="quiz__drawer"></div> </body>

Here is an example for you to try, hope this helps

 $('body').on("click", '.step2-opt', function () { // get the height of step2info var height = $('.step2info').css("height"); // set the height of quiz__drawer $('.quiz__drawer').css("height", height) });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="step2info" style="height: 200px; width: 200px; background-color: red;"></div> <div class="quiz__drawer" style="height: 100px; width: 200px; background-color: green;"></div> <button class="step2-opt">set height</button>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM