Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

focusout() and trigger.('focusout') not causing input to lose focus

I'm using the jQuery UI modal dialog, and populating it with some form fields. Because of this bug: http://bugs.jqueryui.com/ticket/4731, the first input gains focus when the dialog opens. To get around that, I'm trying to blur the affected input when the dialog is opened.

The problem is that there exists other functionality which is called for this input on a .blur(), and I don't want to fire that functionality for this.

So, I'm trying to use .focusout()and .trigger('focusout') to achieve the same effect, but with no results.

Doing either of these:

$('#input-id').focusout();
$('#input-id').trigger('focusout');

does not actually cause the input to lose focus, where using .blur() is successful. Am I missing something, or is there another way to accomplish what I need?

like image 786
Kevin Whitaker Avatar asked Feb 15 '12 19:02

Kevin Whitaker


People also ask

How do you validate input field while Focusout?

The focusout() method in jQuery is used to remove the focus from the selected element. Now we will use focusout() method to validate the input field while focussing out. This method checks whether the input field is empty or not. Also, use some CSS property to display input field validation.

What does Focusout do in JavaScript?

The focusout event occurs when an element (or any elements inside it) loses focus. The focusout() method attaches a function to run when a focusout event occurs on the element, or any elements inside it. Unlike the blur() method, the focusout() method also triggers if any child elements lose focus.

What is the difference between jQuery Focusout () and Blur () events?

The focusout event fires when an element is about to lose focus. The main difference between this event and blur is that focusout bubbles while blur does not.

How do you remove input field focus?

The blur() method removes focus from an element.


2 Answers

If you fancy a quick javascript hack you could use:

$(':focus').blur();

like image 102
Erwin Avatar answered Oct 06 '22 01:10

Erwin


I used this as a workaround:

$('body').focus();
like image 43
Kyle Suss Avatar answered Oct 06 '22 01:10

Kyle Suss