I recently had a question about how to correctly check if an element exists with jQuery. I found the answer from here:
https://learn.jquery.com/using-jquery-core/faq
Is more simple to check if exists and , if we want more info from the object we will not receive null object error.
if (!!$( "#myDiv" )) { //do something }