jQuery API

.blur()

.blur( handler(eventObject) ) Returns: jQuery

Description: Bind an event handler to the "blur" JavaScript event, or trigger that event on an element.

  • version added: 1.0.blur( handler(eventObject) )

    handler(eventObject)A function to execute each time the event is triggered.

  • version added: 1.4.3.blur( [eventData], handler(eventObject) )

    eventDataA map of data that will be passed to the event handler.

    handler(eventObject)A function to execute each time the event is triggered.

  • version added: 1.0.blur()

This method is a shortcut for .bind('blur', handler) in the first two variations, and .trigger('blur') in the third.

The blur event is sent to an element when it loses focus. Originally, this event was only applicable to form elements, such as <input>. In recent browsers, the domain of the event has been extended to include all element types. An element can lose focus via keyboard commands, such as the Tab key, or by mouse clicks elsewhere on the page.

For example, consider the HTML:

<form>
  <input id="target" type="text" value="Field 1" />
  <input type="text" value="Field 2" />
</form>
<div id="other">
  Trigger the handler
</div>
The event handler can be bound to the first input field:
$('#target').blur(function() {
  alert('Handler for .blur() called.');
});

Now if the first field has the focus, clicking elsewhere or tabbing away from it displays the alert:

Handler for .blur() called.

To trigger the event programmatically, apply .blur() without an argument:

$('#other').click(function() {
  $('#target').blur();
});

After this code executes, clicks on Trigger the handler will also alert the message.

The blur event does not bubble in Internet Explorer. Therefore, scripts that rely on event delegation with the blur event will not work consistently across browsers. As of version 1.4.2, however, jQuery works around this limitation by mapping blur to the focusout event in its event delegation methods, .live() and .delegate().

Example:

To trigger the blur event on all paragraphs:

$("p").blur();

Support and Contributions

Need help with .blur() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to .blur()? Report it to the jQuery core team.

Found a problem with this documentation? Report it on the GitHub issue tracker

  • dugokontov

    To properly activate blur event on input element that is not focused, you have to call focus() fist.

  • Anonymous

    Consider the following situation: we have 2 input text elements, each have an onblur(), and a div with an onclick(). In the div onclick() I want to give a value to the 1st input element (helper div), on the 1st input’s onblur() I want to hide the div as help is no more needed.

    What happens is that the system thinsk that clicking on the div means that I want to leave the input element, so the input’s onblur() goes first, which hides the div, and the div’s onclick() never runs. How to solve this problem, any idea?

    • http://twitter.com/danilo_haertzer Danilo Härtzer

      I had the same problem.

      Solution 1: use a timeout (but not safe):
      $(element).blur(function() { setTimeout(, 100); });

      Solution 2: track mouseOver for elements that should pre-event blur action:
      var mouseOver = false;
      $(“preEventBlur”)
      .mouseover(function() { mouseOver = true; })
      .mouseout(function() { mouseOver = false; });

      $(element).blur(function() {
      if (mouseOver) {
      return;
      }

      // blur action comes here (e.g. hide)
      });

      These solutions solves my problem, maybe you’ll find an approach for your problem.

      • thomasesmith

        Solution 2 is best for me.

        I wrote my “view cart” page to update the order quantity of that cart item (via ajax) if the user blurs out of the ‘quantity’ field of that item (only of course if the quantity had changed from the original value) and show a quick animation. This was fine until I added a ‘remove from cart’ link (also via ajax, also with an animation) beneath that box for each item in the cart.

        The problem was that if the user focus’d the quantity update field, then changed the quantity, but then decided to instead click the ‘remove from cart’ link, the blur code and animation of the quantity box would execute when the ‘remove’ link was clicked, and THEN the remove code and animation would execute. It worked still, but it was slow and weird looking.

        By using solution 2, I can detect where the qty was blurred out to. If it was with that items ‘remove’ link, it would bypass the quantity update code, and just execute the remove code. And since there are going to be many items on the cart page, instead of one ‘mouseOver’ variable, I set up an array to keep track of the mouseover/mouseout of each ‘remove item’ link, unique to the item ids of the items in the cart:

        mouseOver[item_id] = true;

        Thanks!

    • http://twitter.com/Marcelo_Assis Marcelo de Assis

      I recommend you to give a look at focusin() and focusout() events, they will resolve your problem:

      http://api.jquery.com/focusin/
      http://api.jquery.com/focusout/

      • Jerry Wu

        Can you post some example code here? thanks

    • Jerry Wu
  • adam1920

    Blur event not work on checkbox in Chrome.
    What could be the solution ?
    Thank you, Adam

  • Jerry Wu

    I think here is a better solution

    http://www.jankoatwarpspeed.co…/

  • CronLogic

    In Firefox (haven't tried in other browsers), I can't seem to get .blur to fire on Shift -Tab. Do I need to use a key event handler here instead?

    • CronLogic

      Works with .focusout though. I guess I'll go with that.

  • Honar Jems

    in blur can i use image tags
    like this
    $(“#txt_username”).blur(function(){
    $(“txt_uername”).append(“<img src=”worng.jpg”>”)
    });

    • Asd

      hey

  • Doanthich

    Hi hi!
    I have a website that I've been using jquery.
    Where you can see here.
    http://vietnamtravel-destinati… .