From what I understand, when we write JQuery code inside $(document).ready(), the code will only be executed after the DOM has finished rendering.
I encountered an issue where I was trying to calculate the height of a div inside $(document).ready() but it kept returning zero. Surprisingly, when I ran the same code in the browser's console, it returned the correct height.
The content is being loaded through AngularJS.
JQuery:
$(document).ready(function (){
// hide view more button if user bio content is less than 200 words
var userBioLength = $('.userBioText > p').html().split(" ").length;
if( userBioLength <= 200){
$('.userBioOverly').addClass("hide");
$('.viewMore').addClass("hide");
var textHeight = $(".userBioText").height();
console.log(textHeight); // returns 0
$('.userBioWrapper').css({ height: textHeight + 10 + 'px' });
}
});
HTML:
<div class="userBioWrapper">
<div class="userBioText">
<p>
{{userInfo.bio}}
Lorem ipsum dolor sit amet consectetur adipisicing elit.
Id fugiat praesentium molestias aliquam quam cupiditate officiis eligendi dolor repellendus,
recusandae voluptatem. Iste aliquam itaque rem tempore officia perspiciatis natus molestiae.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Id fugiat praesentium molestias
aliquam quam cupiditate officiis eligendi dolor repellendus, recusandae voluptatem.
Iste aliquam itaque rem tempore officia perspiciatis natus molestiae.
</p>
</div>
<div class="userBioOverly"></div>
</div>
I'm puzzled by this inconsistency in behavior. Could I possibly be making a mistake somewhere?