How to operate on a certain element when blur?
Question
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.
Solution
Blur will keep the context as you'd expect:
$("#target").blur(function() {
$(this).text("See? It works!");
});
OTHER TIPS
var foo = $('target');
foo.focus(function(){foo.....});
The anonymous function will act as a closure and will remember the value of foo.
I'm able to access 'this' with blur, the following code is working ok...
$(".aClass").blur(
function(){
alert($(this).attr("id"));
});
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.