jQuery Stop .blur() event when clicking "submit" button

Jake picture Jake · Dec 5, 2012 · Viewed 17.9k times · Source

I am building a small landing page with a simple demo e-mail signup form. I want to have the form field open up when focused, and then shrink back down on blur.

However the problem I'm facing is when you click the submit button this instigates the blur function, hiding the button and shrinking the form. I need to find a way to stop the .blur() method only when the user is clicking to focus on the submit button. Is there any good workaround for this?

Would appreciate any help I can get!

Answer

William Neely picture William Neely · Nov 7, 2013

I know this question is old but the simplest way to do it would be to check event.relatedTarget. The first part of the if statement is to prevent throwing an error if relatedTarget is null (the IF will short circuit because null is equivalent to false and the browser knows that it doesn't have to check the second condition if the first condition is false in an && statement).

So:

if(event.relatedTarget && event.relatedTarget.type!="submit"){
     //do your animation 
}