Modermo Modermo - 4 months ago 45
jQuery Question

Input won't focus when element opens

Trying to get an input to focus when the parent container has the class open.

Code I have so far is:


Which, only works oddly when the element begins to close. So, I changed it to:


Which doesn't work at all. Any ideas?

Codepen here


I updated your CodePen.

You have an animation going on...
I didn't checked how it is achieved closely, since an external lib (TweenMax) is doing it.

But, seeing the animation, I thought about delay.
And this is my answer.

You can't focus an element that has display setted to none.
This display is setted to block or inline-block by your animation somewhere...

I found that a 10ms delay was enought in this case.
And just to be sure we select the right input, because even if there is only one in that example... Your site will have more for sure.

I specified it to be a children of a sibling of the clicked element.

Here is the relevant code:

// This is the code for the input focus
    var searchInput = $(this).siblings().find("input");