简体   繁体   中英

Swapping input field placeholder text with jQuery

I try as following jquery code, but it doesn't work true, how can fix it?

Demo: http://jsfiddle.net/pjqcf/

<input type="text" value="Some descriptive text...">​

$('input[type=text]').focus(function () {
    if ($(this).val() == $(this).attr('defaultValue')) {
        $(this).val('');
    }
});
$('input[type=text]').blur(function () {
    if ($(this).val() == '') {
        $(this).val($(this).attr('defaultValue'));
    }
});​

You haven't set "defaultValue" attribute of your input. Set "defaultValue" to "Some descriptive text.." and it should work

<input type="text" value="Some descriptive text..." defaultValue="Some descriptive text...">​

Works fine after adding defaultValue to your markup. See below change,

<input type="text" 
    value="Some descriptive text..." 
    defaultValue="Some descriptive text...">

DEMO

change "defaultValue" to "value". Or add defaultValue attribute to your input

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM