Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if div has html elements inside

Tags:

jquery

I was just wondering how can I check if a div has no children because I need to place a placeholder text if the element does not load into the specified div.

Here is my code:

var test = ($('#'+chartId)).children().length;
console.log(test);
if(test==""){
    alert('empty');
}
else{
    alert('present');
}

Will this work if I'm trying to detect if a flash component is being loaded to a div?

like image 362
a_miguel6687 Avatar asked Jan 31 '26 08:01

a_miguel6687


1 Answers

.length is 0 and not empty when there are no childrens. Also you can shorten your code a bit, like so:

if(!($('#'+chartId)).children().length){
    alert('empty');
}
else{
    alert('present');
}
like image 141
RononDex Avatar answered Feb 02 '26 00:02

RononDex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!