Friday, April 25, 2014

setup Placeholder by jquery

function setupPlaceholder(inputid) {
    if ($.browser.webkit) return false; var target = $('#' + inputid);
    if (target.length == 0) { target = $('input[type="text"], input[type="email"], input[type="search"]'); }
    target.each(function (i, el) {
        el = $(el);
        var ph = el.attr('placeholder');
        if (!ph) return true;
        el.addClass('placeholder');
        el.attr('value', ph);
        el.attr('style', 'color:grey');
        el.focus(function (e) {
            if (el.val() == ph) {
                el.removeClass('placeholder');
                el.attr('value', '');
                el.attr('style', 'color:black');

            }

        });
        el.blur(function (e) {
            if ($.trim(el.val()) == '') {
                el.addClass('placeholder');
                el.attr('value', ph);
                el.attr('style', 'color:grey');

            }
        });
    });
}

No comments:

Post a Comment