i have create a div class=box via Ajax call and appended to an main div.
This div class=box change its height based on its content. I need to have its height but .height() return 0….
$.ajax({ url: "json/news.json",
dataType: "json",
contentType: "application/x-www-form-urlencoded;charset=UTF-8",
success: function(data){
var html ="<div class='box'>"+data.box+"</div>";
}
});
// now i want its height
var j = $('.box').height();
Any suggestion? Thanks
There are 2 problems here:
$.ajaxis asynchronous, so it does not create anything until thesuccesscallback has run. If you want to measure your box, you will need to do it in the callback:You are not adding your html to anything, and as it is not part of the DOM it will not have a height. You need to do something like the below: