2

My code doesn't run and I want the value to be cleared when you click on another

what am I doing wrong?

http://jsfiddle.net/AL2vS/

var placeholderText = $('input').val();

$(placeholderText).focus(function() {
    $(this).attr('value', "");
});
$(placeholderText).blur(function() {
    $(this).attr('value', phTextVal);
});​

6 Answers 6

4

You're using 'placeholderText' as a selector, which won't select anything. Use $('input').focus(... and $('input').blur(... instead.

UPDATE

If you want to store the existing value, and then replace it, then store it as .data() in the <input> element itself:

$('input').focus(function() {
    $this = $(this);
    $this.data('oldval',$this.val());
    $this.val('');
});
$('input').blur(function() {
    $this = $(this);
    $this.val($this.data('oldval'));
});​

http://jsfiddle.net/mblase75/AL2vS/12/

Or possibly:

$('input').blur(function() {
    $this = $(this);
    if ($this.val().length==0) { // only if the field is blank
        $this.val($this.data('oldval'));
    };
});​
Sign up to request clarification or add additional context in comments.

4 Comments

jsfiddle.net/AL2vS/6 I did that but now everything shows up as my first value attribute.
Fixable, but I don't understand why you want to overwrite the user's text as soon as he blurs out of the field.
I had edited your code but if I wanna keep the original placeholder text I dont know how to do that jsfiddle.net/Nz2GH/4
@webdevgirl Consider the updated answer, or post a new question.
0

Do you mean like this?

http://jsfiddle.net/AL2vS/1/

Comments

0

This will at least get the removal of text working:

var placeholderText = $('input');

placeholderText.focus(function() {
    $(this).attr('value', "");
});
placeholderText.blur(function() {
    $(this).attr('value', phTextVal);
});​

You could also take a look at the HTML5 placeholder param.

1 Comment

yes I know the html5 placeholder attribute will do that automatically but I wanted to learn the jQuery way
0

Check it out,

$('input').focus(function() {
    placeholderText = $('input').val();
    $(this).val("");
});

$('input').blur(function() {
    $(this).val(placeholderText);
});​

http://jsfiddle.net/AL2vS/8/

you still need to do something with the data that is entered.

Comments

0

I guess the better way is:

var placeholderText = $('input');

$(placeholderText).focus(function() {
    phTextVal = $(this).val();
    $(this).val("");
}).blur(function() {
    $(this).val(phTextVal);
});​

Comments

0

How about using object.defaultValue property?

function:

$.fn.smartInput=function(){
    return this.each(function(i,o){
     $(o).focus(function(){ if(this.value==this.defaultValue) this.value='';})
         .blur(function(){ this.value=(this.value=='')?this.defaultValue:this.value;});
     });

}

usage:

$('input').smartInput();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.