Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you trigger a blur/focusout event on an input text box using jquery?

I'm using 1.7.2 in which I understand this should work:

// cell is a jquery representation of a <td> element
cell.append($("<input/>", { "type": "text" }).val(content));        
cell.children()[0].focus();
cell.children()[0].on("blur", function() {
    alert("blur");
}

The input box is appended, grabs focus and then the javascript console tells me:

Uncaught TypeError: Object #<HTMLInputElement> has no method 'on'

I'd be grateful if anyone knows how I can catch the blur/focusout event.

like image 822
rich Avatar asked Jun 02 '12 12:06

rich


People also ask

Does blur trigger Focusout?

The blur event fires when an element has lost focus. The main difference between this event and focusout is that focusout bubbles while blur does not. The opposite of blur is focus . This event is not cancelable and does not bubble.

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

The main difference between this event and blur is that focusout bubbles while blur does not. The opposite of focusout is focusin .

What is blur event in jQuery?

jQuery blur() Method The blur event occurs when an element loses focus. The blur() method triggers the blur event, or attaches a function to run when a blur event occurs. Tip: This method is often used together with the focus() method.

What is blur () method?

blur() method removes keyboard focus from the current element.


1 Answers

You are accessing the DOM element with [0] and acting like it is a jQuery object.

If you only want the first, you need to use eq() to get the jQuery object.

cell.children().eq(0).focus().on("blur", function() {
    alert("blur");
};
like image 121
epascarello Avatar answered Oct 31 '22 21:10

epascarello