views:

38

answers:

3

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);
    });
});
A: 

i usually use this code which works :)

http://www.eggchops.com/web-stuff/jquery/jquery-clear-focus-function/

here's the code:

$(document).ready(function(){

var clearMePrevious = ”;

// clear input on focus
$(’.clearMeFocus’).focus(function() {
  if($(this).val()==$(this).attr(’title’)) {
    clearMePrevious = $(this).val();
    $(this).val(”);
  }
});

// if field is empty afterward, add text again
$(’.clearMeFocus’).blur(function() {
  if($(this).val()==”) {
    $(this).val(clearMePrevious);
  }
});
});
corroded
Keeping just one variable for every field is the problem he already has.
Pointy
im suggestng he just use this function. just add a clearMeFocus class on the textfield and it works no matter how many it is
corroded
I see what you mean. Well I wouldn't do it that way, but it probably would work OK. It's certainly better than the original.
Pointy
+3  A: 

The explanation for why your code works the way it does is kind-of complicated. For every input field, you're establishing (and re-establishing, on every "focus" event!) handlers for "blur" events on all the inputs. It's confusing and hard to think about, so I'll just sum it up and say "don't do it that way."

adaugaInput.focus(function() {
  var input = $(this);
  if (!input.data('default')) input.data('default', input.val());
  if (input.val() === input.data('default'))
    input.val('');
});

adaugaInput.blur(function() {
  var input = $(this);
  if (input.val() === '') input.val(input.data('default'));
});

Note that I use ".val()" to get/set the "value" attribute of the input fields. Also, the "blur" handler is set up outside the "focus" handler. This code uses the jQuery ".data()" mechanism to keep the per-element default value. Not tested but it's probably pretty close.

With a mechanism like this, it's sometimes nice to re-style the input so that (for example) the default value shows up with a lighter font color. To do that, you'd remove and add a class to the input field, and affect the style with CSS. (The inputs would have to start off with the class; or I suppose you could apply the class on focus.)

Pointy
+1 This is probably the correct way the implementation should work. Plus, I've just *borrowed* the code for a page on my own project. :)
GenericTypeTea
Thanks, this worked. The only reason I used blur inside focus was because of the defaultValue variable, which I couldn't use if blur was outside.
Norbert
Right, well I'm a big fan of ".data()" :-)
Pointy
A: 

I'd go the other way around, saving defaultValue first, then binding focus:

var adaugaInput = $('form#adauga input:text');

adaugaInput.each(function(){
    var defaultValue = $(this).val();
        $(this).focus(function() {

        if($(this).attr("value") == defaultValue) $(this).attr("value", "");
    });
    $(this).blur(function() {
        if($(this).attr("value") == "") $(this).attr("value", defaultValue);
    });
});

Standard disclaimers apply, the above code is not tested. (edit: fixed mulitple blur binds)

Kristoffer S Hansen
I don't think you should be re-applying the "blur" handler on each call to the "focus" handler!
Pointy