How, or is it even possible, to get the this value from a function? I’ve made this snippet which saves defaultValue on the function, but I would like to read this from outside the function, directly on the dom element. Is that possible??
I’ve made this, in jQuery:
$("input").val(function() {
var $label = $("label[for='" + $(this).attr("id") + "']");
this.defaultValue = $label.text();
$label.hide()
return this.defaultValue
}).click(function() {
if ($(this).val() == this.defaultValue) {
$(this).val("");
}
}).bind("blur", function() {
if ($(this).val() == "") {
$(this).val(this.defaultValue);
}
});
See it in action here: http://jsfiddle.net/ZUZ3L/g6dMA/
You can save it to the element represented in the DOM with jQuery’s .data() function:
Do this on pageLoad:
You can retrieve it later with:
…which will return the value set at pageload, rather than the current value of the input.