0

Possible Duplicate:
Getting the ID of the element that fired an event using JQuery

To give an example suppose I have

<p id="name" class="editable"...

Later on in JavaScript I have

$("#editable").focusout(function(e){...

How can I retrieve the id of the element that has just lost the focus?

Community
  • 1
  • 1
Hoa
  • 19,858
  • 28
  • 78
  • 107

2 Answers2

3

You have wrong selector in jQuery, must be:

$(".editable")

To alert id of element that lost focus you need to use this context as selector inside callback:

$(".editable").focusout(function() {
    alert($(this).attr('id'));
});
antyrat
  • 27,479
  • 9
  • 75
  • 76
1
$(".editable").focusout(function(e){
    var id = $(this).attr('id');
});

Or, if the .editable element is just a wrapper and the interesting element (input) is a child of it, then:

$(".editable").focusout(function(e){
    var id = $(e.target).attr('id');
});​

Demo: http://jsfiddle.net/sveinatle/MWvAV/1/

Supr
  • 18,572
  • 3
  • 31
  • 36