[jQuery] Thuộc tính Placeholder trong HTML5

http://www.nhatchanh.info/2014/08/thuoc-tinh-placeholder-text-trong-html5.html

by Phan Nhật Chánh

HTML

<input type='text' placeholder='Tìm kiếm...'>
<br>
<textarea placeholder='Nhập nội dung vào đây...' cols='50' rows='10'></textarea>

CSS

input.blur,
textarea.blur {color:#bbb}

JavaScript

$('[placeholder]').each(function() {
    var plc = $(this).attr('placeholder');
    $(this).addClass('blur').removeAttr('placeholder').val(plc).on("focus blur", function(e) {
        $(this)[(e.type == "blur" && (this.value === "" || this.value == plc)) ? "addClass" : "removeClass"]('blur');
        if (e.type == "blur" && this.value === "") $(this).val(plc);
        if (e.type == "focus" && this.value == plc) $(this).val("");
    });
});