onfocusoutEvent

事件对象参考手册Event Object

Examples

Execute JavaScript when the input field is about to lose focus:

<input type="text" onfocusout="myFunction()">

Try it Yourself »

More examples are included at the bottom of this chapter.


Definition and Usage

The onfocusout event fires when an element is about to lose focus.

Tip:The onfocusout event is similar toonblurthe event. The main difference is that the onblur event does not support bubbling. Therefore, if you need to check whether an element or its child element loses focus, you need to use the onfocusout event.

Tip:Although Firefox does not support the onfocusout event, you can useonfocus(the optional parameter of the addEventListener() methoduseCapture) capture listener event to check whether an element or its child element has lost focus.

Tip:The opposite event of the onfocusout event isonfocusinthe event.


Browser Support

Event
onfocusout Yes Yes Yes Yes Yes

Note:Using the onfocusin event with HTML DOM syntax in Chrome, Safari, and Opera 15+ browsers may not work properly. However, as an HTML element, it can work properly by using the addEventListener() method.


Syntax

In HTML:

<element onfocusout="myScript">Try it Yourself

In JavaScript (may not work properly in Chrome, Safari, and Opera 15+):

object.onfocusout=function(){myScript};Try it Yourself

In JavaScript, using the addEventListener() method:

object.addEventListener("focusout", myScript);Try it Yourself

Note:Internet Explorer 8 and earlier IE versions do not supportaddEventListener()the method.


Technical Details

Bubbles: Yes
Cancelable: No
Event type: FocusEvent
Supported HTML tags: All HTML elements, except: <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, and <title>


实例

More Examples

Example

Using the "onfocusin" and "onfocusout" events:

<input type="text" onfocusin="focusFunction()" onfocusout="blurFunction()">

Try it Yourself »

Example

Event delegation: set addEventListener()'suseCaptureparameter to true (for gaining and losing focus):

<form id="myForm">
  <input type="text" id="myInput">
</form>

<script>
var x = document.getElementById("myForm");
x.addEventListener("focus", myFocusFunction, true);
x.addEventListener("blur", myBlurFunction, true);

function myFocusFunction() {
    document.getElementById("myInput").style.backgroundColor = "yellow";
}

function myBlurFunction() {
    document.getElementById("myInput").style.backgroundColor = "";
}
</script>

Try it Yourself »

Example

Event delegation: using the focusout event (not supported in Firefox):

<form id="myForm">
  <input type="text" id="myInput">
</form>

<script>
var x = document.getElementById("myForm");
x.addEventListener("focusin", myFocusFunction);
x.addEventListener("focusout", myBlurFunction);

function myFocusFunction() {
    document.getElementById("myInput").style.backgroundColor = "yellow";
}

function myBlurFunction() {
    document.getElementById("myInput").style.backgroundColor = "";
}
</script>

Try it Yourself »


事件对象参考手册Event Object

Other Extensions