tags:

views:

48

answers:

4

When focusing,we can do something like:

$('#target').focus(function(){$(this)..});

Because "this" is just the one that's focused($(this) == $('#target')).

But can't do the same for "blur",because $(this) != $('#target').

How to do it the right way?

NOTE: in my application,I can't assign an id to the target,and '#target' here is just for illustration.

A: 

var foo = $('target'); foo.focus(function(){foo.....});

The anonymous function will act as a closure and will remember the value of foo.

bandi
In fact I can only use class name as selector,and there are multiple ones with that class name.
Shore
A: 

I'm able to access 'this' with blur, the following code is working ok...

$(".aClass").blur(
  function(){
    alert($(this).attr("id"));
});
daddywoodland
+1  A: 

Blur will keep the context as you'd expect:

$("#target").blur(function() {
    $(this).text("See? It works!");
});
peirix
A: 

The Event/blur triggers the blur event of each matched element and $(this) return just the one that's blured.

jQuery("#target").blur(function() {
  console.log( jQuery(this) );
});

Use Firebug. It also add a global variable named "console" to all web pages loaded in Firefox. In this case "console.log" writes a message to the console.

ranonE