Set div width equal to another div

I want to specify the .second_div width of the first_div , but the problem is that the first div doesn't have the width value specified in css, and I need to find its width using jQuery.

HTML:

 <div class="first_div">First Div</div> <div class="second_div">Second.width == First.width</div> 

CSS

 .second_div, .first_div { border: 1px solid black; padding: 2px 4px; display:inline-block; } 

JQuery

 $(document).ready(function(){ $(".second_div").css({'width':($(".first_div").width()+'px')}); }); 

jsfiddle: https://jsfiddle.net/ekqecjb8/1/

+6
source share
1 answer

There is no jQuery library in your fiddle, there is no error in your code. Just include jQuery library in your code

 $(document).ready(function() { $(".second_div").css({ 'width': ($(".first_div").width() + 'px') }); }); 
 .second_div, .first_div { border: 1px solid black; padding: 2px 4px; display: inline-block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="first_div">First Div</div> <div class="second_div">Second.width == First.width</div> 
+4
source

All Articles