Reputation: 743
i use alert to check if a div has any child controls:
alert(document.getElementById("MainContent_imgContainer").hasChildNodes());
and this always returns true even if the MainContent_imgContainer div doesn't have any child controls,
please tell me what is the better way to determine if a div has child controls.. thanks
Upvotes: 0
Views: 204
Reputation: 3630
Check the .length
of the selector to see how many elements it matched, in this case:
if($("#divid").children().length) {
//child exists
}
Upvotes: 0
Reputation: 13994
Probably your container has a space, which counts as a textNode. Ensure your div is really empty.
Or use another mechanism, e.g. jQuery, that only counts non-textNodes
$("#MainContent_imgContainer").children().length; // 0 when empty
Upvotes: 2
Reputation: 66663
Use .children.length
instead which is supported across all browsers. MDN Docs
alert(document.getElementById("MainContent_imgContainer").children.length > 0);
Upvotes: 1