I have a jQuery script below that clears the value of an input on focus and puts it back on blur if the input remains empty. It works great, but here's the problem:
Let's say I have 3 input fields on the same page. I click on the first one with the value "Name", defaultValue variable becomes "Name" and the field clears. If I click out, the value goes back to "Name". Now if I click on the second field without refreshing the page, it clears just fine, but when I click outside, instead of getting the value "Initials", it gets the value of the first field.
So how can I get the defaultValue variable to update itself every time I click on a field?
var adaugaInput = $('form#adauga input:text');
adaugaInput.focus(function() {
var defaultValue = $(this).val();
if($(this).attr("value") == defaultValue) $(this).attr("value", "");
adaugaInput.blur(function() {
if($(this).attr("value") == "") $(this).attr("value", defaultValue);
});
});