NunuJ
NunuJ

Reputation: 49

Call focus() after jQuery append() is completed

I have an append() and a focus() which is set to focus the first input in the appended dom.

IE is taking time to append and so the focus is not working. Is there a way (other than setTimeout) to set the focus after the dom is fully appended?

Thanks!

Upvotes: 0

Views: 5386

Answers (2)

totten
totten

Reputation: 2817

var oldHtml, el, toBeAppended;
function appendAndFocus()
{
   oldHtml = el.html();
   el.append(toBeAppended);
   appnedAndFocus_h()
}
function appnedAndFocus_h()
{
   if(el.html()==oldHtml) //if(el==oldHtml) - CORRECTED.
   {
      setTimeout(appendAndFocus_h, 10); //just for browser to be taken a breath.
      return;
   }
   else
   {
      el.focus();
   } 
}

Notice : Not tested code.

Upvotes: 0

Ian Bishop
Ian Bishop

Reputation: 5205

append() and appendTo() are synchronous commands so there is no need for a callback.

$(child).appendTo(parent)
        .focus();

If you are using multiple append() statements, this is actually considerably slower than simply appending something big once. That may explain why you are finding it is taking awhile in IE.

Upvotes: 1

Related Questions