uag-va
Last Updated: February 25, 2016
·
731
· devers
D27d0237833f17bd8c503b038c7bbcf2

Clear inputs that have default text

Sometimes the HTML5 placeholder attribute isn't ideal. In those situations, it's pretty each to add this JS to your page (make sure to also include jQuery) to cover all your text/email inputs

$("input[type='text'], input[type='email']").focus ->
    content = @getAttribute("value")
    $(this).val "" if $(this).val() is content

$("input[type='text'], input[type='email']").blur ->
    content = @getAttribute("value")
    $(this).val content if $(this).val() is ""

or if coffeescript isn't your thing

$("input[type='text'], input[type='email']").focus(function() {
    var content;
    content = this.getAttribute("value");
    if ($(this).val() === content) {
      return $(this).val("");
    }
});

$("input[type='text'], input[type='email']").blur(function() {
    var content;
    content = this.getAttribute("value");
    if ($(this).val() === "") {
        return $(this).val(content);
    }
});
Say Thanks
Respond