I have two div with img1 and img2 ids respectively. I want to check either div contain further HTML element or not, it is giving me that HTML is not empty, while it is empty, as you can see it in code.
As i click on the upload button, it alert me with message "not empty", even #img1 is empty.
My Question why it is executing wrong condition?
$(document).ready(function(e) {
$('#upload').on('click',function(){
if($('#img1').html()==''){
$('#img1').addClass('TuyoshiImageUpload_div');
$('#img1 input[name=image]').trigger('click');
}else{
alert('not empty');
}
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="img1" class="col-sm-3" style=" border:1px solid red; height:50px; width:50px; float:left"></div>
<div id="img2" class="col-sm-3" style=" border:1px solid red; height:50px; width:50px; float:left; margin-left:15px"></div>
<div>
<input type="button" value="click me" id="upload" />
</div>
$('#img1').html()?#img1 input[name=image]as a selector, it suggests that at least some of the time, the #img1 div is not empty. Check that if you are adding to and removing from the div's contents, your removal process is total, including any whitespace.