tags:

views:

23

answers:

3

Hey all. Basically I have a page full of text input fields with default values in them.

When a user focuses on the input, I want the value to be set to nothing, but then if the user focusses out (on blur) and the input is still empty, I want to change its value back to what it was originally.

At the moment I have this, but it isn't working:

$("#login input").focus(function(){
    $(this).val("");
      $(this).data({
       value: $(this).val()
      });
}); 
$("#login input").focusout(function(){
  if($(this).val()=="") {
   $(this).val($(this).data("value"));
  }
});

What I need is some sort of NOT .change() kind of thing? Maybe?

Thanks in advance

+3  A: 

Re-write it a bit (reverse the order in the focus!), like this:

$("#login input").focus(function(){
    $.data(this, "value", $(this).val());
    $(this).val("");
}); 
$("#login input").focusout(function(){
  if($(this).val()=="") {
   $(this).val($.data(this, "value"));
  }
});

The $.data() shortcut is a bit neater and less wasteful (no extra jQuery objects)...but the main problem is you need to swap the order in the focus, you were clearing the value before storing it, so it was being stored as "" every time.

Nick Craver
+1 Could you make it a bit clearer that the problem is in the order of statements in the first handler?
MvanGeest
@MvanGeest - Done :)
Nick Craver
Awesome! Should've seen that myself really :D Thanks for the info on $.data as well.
Tim
IMHO `$(this).data("value", $(this).val());` is slightly better readable.
jigfox
@Jens - Readable isn't always better...you just wasted creating an extra jQuery object that way :)
Nick Craver
you're right, but you can do it like this: `var $this = $(this); $this.data("value", $this.val()); $this.val("");`
jigfox
A: 

it's not working because first you set an empty value and then you store that empty value. Try this:

$("#login input").focus(function(){
  $(this).data({
    value: $(this).val()
  });
  $(this).val("");
}); 
$("#login input").focusout(function(){
  if($(this).val()=="") {
   $(this).val($(this).data("value"));
  }
});
andu
A: 
<div id="login">
<input type="text" data-default="default value"/>
</div>

<script type="text/javascript">
$("#login input).focus(function(){
  var $this = $(this);
  if($this.val() == $this.attr('data-default')) $this.val("");
}).blur(function(){
  var $this = $(this);
  if($this.val() == "") $this.val($this.attr('data-default'));
}).blur();
</script>

This way you can put another than default value in the fields, without getting them removed. i.e. the user enters a false password you could pre fill the username with the one provided by the user in the first attempt. if you do it your way. this value will be removed if i focus on this field.

jigfox
Instead of a `.each()` with duplicated code you can replace your entire `.each(...)` with `.blur()`, just trigger the handler you attached, no need to put the same code in twice.
Nick Craver
@Nick Craver: Thanks for the advice. I will change my code.
jigfox