1

I have tried to make a jQuery code to delete the value of my (email) input field when clicked, and when unclicked (.blur()) the value (same as before) should return to the input field, unless something else is written. It works when I click the input field, the text disappears and comes back when I click away, but when i write something and deletes it, the value/text from before doesn't come back.. Heres my code, applied to a simple input type="email"

$(document).ready(function() {
var default_email = 'Skriv din email her..';

$('input[type="email"]').attr('value', default_email).focus(function() {
if ( $ (this).val() == default_email) {
    $(this).attr('value', '');
}
}).blur(function() {
if($(this).val() == '') {
    $(this).attr('value', default_email);
};
});

});
0

2 Answers 2

1

You are trying to make your own placeholder. Nice, but more natural and effective is to use native:

<input type="email" placeholder="Skriv din email her.." >

Demo: http://jsfiddle.net/E9EzZ/2/

If you anyway want to create your own version then fix your code as follows:

var default_email = 'Skriv din email her..';

$('input[type="email"]').val(default_email)
.focus(function () {
    if (this.value == default_email) {
        this.value = '';
    }
})
.blur(function () {
    if (this.value == '') {
        this.value = default_email;
    };
});

Demo: http://jsfiddle.net/E9EzZ/1/

Sign up to request clarification or add additional context in comments.

Comments

1

Check this fiddle: http://jsfiddle.net/LfNZu/ Just change function attr() to val()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.