7

I have a email form text box that while it is empty I would like it to have the value "E-Mail" And when you click on it the text goes away. If someone clicks on it and doesn't enter text. on Blur I would like for it to return to having the default text.

I have been trying a few things but nothing is working. Could someone please point me in the right direction?

7 Answers 7

20

Or you could just use the placeholder html5 attribute:

<input type="text" id="keyword" name="keyword" placeholder="Type keyword here" />
Sign up to request clarification or add additional context in comments.

3 Comments

Didn't know about the placeholder attribute. Great addition! :) It's a shame IE doesn't support it at the moment (IE 9).
Does one still consider IE to be a web browser??
By now the placeholder attribute is supported by all browsers: caniuse.com/input-placeholder (even IE!)
9

it goes something like this

$('#yourElement').focus(function(){
    //Check val for email
    if($(this).val() == 'E-Mail'){
        $(this).val('');
    }
}).blur(function(){
    //check for empty input
    if($(this).val() == ''){
        $(this).val('E-Mail');
    }
});

Comments

5

you can use the placeholder attribute and then us this jquery as backup for IE

<input type="text" id="keyword" name="keyword" placeholder="The watermark" value='The watermark' class="watermark"/>

$(document).ready(function () {
    $('.watermark').focus(function () {
        if ($(this).val() == $(this).attr('placeholder')) {
            $(this).val('');
        }
    }).blur(function () {
        if ($(this).val() == '') {
            $(this).val($(this).attr('placeholder'));
       }
    });
});

Comments

3

It's pretty straightforward. Just erase the value onfocus, and then (if the value is still empty) refill it onblur.

1 Comment

The example also removes newly entered text in the input field, which is not what you'd want.
1

Call the below function and pass it two args: slightLabel(jQuery('#email_field'), 'email');

  function slightLabel(input, text) {
        jQuery(input).val(text);
        jQuery(input).data('defaultText', text);
        jQuery(input).focus(function(){
            if(!jQuery(this).data('touched'))
            {
                jQuery(this).data('touched', true);
                jQuery(input).val('');
            }
        });

        // the part to restore the original text in
        jQuery(input).blur(function(){
            if(jQuery(this).val() == '')
            {
                jQuery(this).val(jQuery(this).data('defaultText'));
            }
        });

    }

Comments

1

You could use one of the watermark plugins of jquery that do just that. I use a watermark plugin that has the following code

$.fn.watermark = function (c, t) {
var e = function (e) {
    var i = $(this);
    if (!i.val()) {
        var w = t || i.attr('title'), $c = $($("<div />").append(i.clone()).html().replace(/type=\"?password\"?/, 'type="text"')).val(w).addClass(c);
        i.replaceWith($c);
        $c.focus(function () {
            $c.replaceWith(i); setTimeout(function () { i.focus(); }, 1);
        })
            .change(function (e) {
                i.val($c.val()); $c.val(w); i.val() && $c.replaceWith(i);
            })
            .closest('form').submit(function () {
                $c.replaceWith(i);
            });
    }
};
return $(this).live('blur change', e).change();

};

Callable in jquery by setting the class of the input textbox to watermark like this

<input type="text" id="keyword" name="keyword" class="watermark" style="width: 250px"
    title="Type keyword here" />

The title is what will be displayed in the watermark.

Comments

1

Use this Jquery-placeholder plugin

Using this plugin makes it possible to also use the placeholder attribute in non HTML5 capable browsers.

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.