Wait for element to exist using a jQuery promise.
$.when($('selector')).then( // do stuff that can only be done when element exists );