-2

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)
afdi5
  • 307
  • 2
  • 13

3 Answers3

1

Example:

Check an object to see if it's empty.

jQuery.isEmptyObject({}); // true
jQuery.isEmptyObject({ foo: "bar" }); // false
Jack Dorset
  • 119
  • 9
1

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")
    }
LucasLaurens
  • 236
  • 1
  • 3
  • 14
0

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>
Deepak A
  • 1,624
  • 1
  • 7
  • 16