Last active
October 1, 2015 16:08
-
-
Save mortennajbjerg/2020940 to your computer and use it in GitHub Desktop.
jQuery HTML5 Placeholder fallback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
'use strict'; | |
/*global jQuery*/ | |
/*global document*/ | |
$(function() { | |
// Make sure this only applies to browsers | |
// without placeholder support | |
if (document.createElement("input").placeholder !== undefined) { return false; } | |
// Loop through all input fields to find those using the HTML placeholder attribute | |
$('INPUT').each(function() { | |
var $this = $(this); | |
var searchboxplaceholder = $this.attr('placeholder'); | |
if(searchboxplaceholder) { | |
// Searchbox placeholder | |
$this.focus(function() { | |
if($this.val() == searchboxplaceholder) { | |
$this.val(''); | |
} | |
}); | |
$this.blur(function() { | |
if($this.val() === '') { | |
$this.val(searchboxplaceholder); | |
} | |
}); | |
} | |
}); | |
}); | |
}(jQuery)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Update where Modenizr detection was removed - and some jslint cleanups