I have the following empty div inside my html page:
<div id='onlineaccess' style='width:20em;'>
</div>
I need to dynamically update this div with html, but before I do this, I need to see if it is empty or not. I wrote the following jQuery and Javascript code for this:
if($('#onlineaccess').is(':empty') )
{alert('No HTML inside of onlineaccess div');}
else
{alert('Some HTML inside of onlineaccess div');}
but this does not give the result I'm looking for if the div is empty.
if($('#onlineaccess').is(':empty') )
{alert('No HTML inside of onlineaccess div');}
else
{alert('Some HTML inside of onlineaccess div');}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='onlineaccess' style='width:20em;'>
</div>
Run codeHide resultThis is a warning about the second message, even if the div is empty. Can someone explain to me why this is so, and how can I get the first message in an alert?
Thanks in advance for your help.
source
share