1

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>

3
  • can you print $('#img1').html()? Commented Dec 22, 2017 at 13:17
  • It's good that you've tried to provide a short code snippet, but that snippet does not exhibit the problem you describe. Given that the code contains #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. Commented Dec 22, 2017 at 14:19
  • I tested your code. Its work correctly! Commented Dec 22, 2017 at 15:08

6 Answers 6

4

There must be some kind of other javascript error on your page which is causing such behavior. Otherwise, your existing code snippet is running perfectly fine without any issue. Try running it.

Sign up to request clarification or add additional context in comments.

Comments

3

You can use :empty selector with .is() method.

if($('#img1').is(':empty'))

$(document).ready(function(e) {
  $('#upload').on('click', function() {
    if ($('#img1').is(':empty')) {
      console.log('empty');
    } else {
      console.log('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" />

Comments

3

I think You Need This

$(document).ready(function(e) {
	
    $('#upload').on('click',function(){
    if ($('#img1').is(':empty')){
    alert('empty');
          $('#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" />

Comments

1

You can use .is().

if( $('#leftmenu').is(':empty') ) {

Or you could just test the length property to see if one was found:

if( $('#leftmenu:empty').length ) {

You can use $.trim() to remove whitespace (if that's what you want) and check for the length of the content.

if( !$.trim( $('#leftmenu').html() ).length ) {

Comments

1

You can use jquery children method and check its length to find if a div is empty

$(document).ready(function(e) {

  $('#upload').on('click', function() {

    //if ($('#img1').html() == '') {
    if ($('#img1').children().length <= 0) {
      $('#img1').addClass('TuyoshiImageUpload_div');
      $('#img1 input[name=image]').trigger('click');
    } else {
      alert('not empty');
    }
  })
});
.TuyoshiImageUpload_div {
  width: 30px;
  height: 40px;
  border: 1px solid green !important;
}

div {}
<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" />

Comments

1

Something really simple you can do, (without having to use jQuery) is the following:

var isElementEmpty = document.getElementById("someDivElement").getElementsByTagName('*').length > 0;

At that point isElementEmpty will be either true of false. This does not get affected by whitespace. It only works with HTML elements.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.