First version :
$(".myClass").focus(function() {
var MyRef=$(this);
if (MyRef.val() == MyRef.attr("rel")) {
MyRef.val("");
}
});
$(".myClass").focusout(function() {
var MyRef=$(this);
if (MyRef.val() == "") {
MyRef.val(MyRef.attr("rel"));
}
});
Second version :
$(".myClass").focus(function() {
if ($(this).val() == $(this).attr("rel")) {
$(this).val("");
}
});
$(".myClass").focusout(function() {
if ($(this).val() == "") {
$(this).val($(this).attr("rel"));
}
});
So, without save this on a variable or use it? Or nothng change?
Caching the current Element in a wrapped jQuery set is recommended practice, as work is required to convert from standard DOM element to a jQuery wrapped set. I would therefore recommend the first approach.
A good approach for clarity is to follow a naming convention when you wrap a DOM element in a jQuery object. Personally i always call it
$this– the dollar tells me its a jQuery object (another convention i use) andthistells me it’s the current element