Diogo Wernik Diogo Wernik - 22 days ago 6
Javascript Question

Jquery change iside div width size, based on outside div size

Hi i would like to change the value of a div width, based on the outside div width

I am new in jquery and javascript and tried this code:

if ($('col-md-4 col-sm-6').width() <= 320 ){
$('.card-info').width(120px)
};


but it didn't work...

the html is:

<div class="col-md-4 col-sm-6">
<div class="card-info">
</div>
</div>
<div class="col-md-4 col-sm-6">
<div class="card-info">
</div>
</div>
(...)


Could some one help me to find out how to do that, change div "card-info" to 120px width if the div "col-md-4 col-sm6" is 320px or less.

thanks a lot.

Answer

You're not selecting the col-md-4 and col-sm-6 class in jQuery. You missed the dot(.) before $('col-md-4 col-sm-6') col-md-4 here and then a comma(,) and the another dot before col-sm-6. The solution would be

    if ($('.col-md-4, .col-sm-6').width() <= 320 ) {
      $('.card-info').width(120); 
    }

or

        if ($('.col-md-4, .col-sm-6').width() <= 320 ) {
          $('.card-info').css('width', '120px'); 
        }
Comments