Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Default text on empty text box

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?

like image 546
CongdonJL Avatar asked Jan 22 '11 21:01

CongdonJL


People also ask

What do you mean by default value in html?

Definition and Usage Note: The default value is the value specified in the HTML value attribute. The difference between the defaultValue and value property, is that defaultValue contains the default value, while value contains the current value after some changes have been made.


3 Answers

Or you could just use the placeholder html5 attribute:

<input type="text" id="keyword" name="keyword" placeholder="Type keyword here" />
like image 78
webtweakers Avatar answered Oct 20 '22 06:10

webtweakers


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');
    }
});
like image 41
Jacob Lowe Avatar answered Oct 20 '22 05:10

Jacob Lowe


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'));
       }
    });
});
like image 5
vts Avatar answered Oct 20 '22 05:10

vts