check id jquery object is empty

Asked by At

I'm trying to check if a jquery object is null to use it this way

var reminder_history_div = $(".history-container");
        if(reminder_history_div)
            reminder_history_div.scrollTop(reminder_history_div[0].scrollHeight);

but reminder_history_div is not null or empty its and object ,with some info ,what is the correct way to check if is empty?maybe:

if(reminder_history_div.length > 0)

3 Answers

0
Community On Best Solutions

Use length to check if element is present or not refer Check if element exists in jQuery

$('.check').click(function(e){

if($('.history-container').length>0)
console.log('Div history-container exist')
else
console.log('Div history-container does not exist')

})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="history-container">
some text
</div>
<button class="check">Check</button>

1
Community On

Example:

Check an object to see if it's empty.

jQuery.isEmptyObject({}); // true
jQuery.isEmptyObject({ foo: "bar" }); // false
1
LucasLaurens On

check if your content is empty or not and if it exists

const reminder_history_div = $(".history-container").html(); //OR val() 

if(!reminder_history_div){
  console.log("empty");
} else {
    console.log("not empty");
}



if($(".history-container").length >= 1){
      console.log("exists");
    } else {
      console.log("don't exists")
    }