Last active
August 29, 2015 13:57
-
-
Save scott-riley/9599668 to your computer and use it in GitHub Desktop.
Changing placeholder via media query
This file contains hidden or 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
// Nice way, using matchMedia, no IE9 or below support | |
var media = "screen and (min-width: 720px)", | |
placeholderShort = "Short holder", | |
placeholderLong = "My quite long placeholder"; | |
$(window).resize(function(){ | |
if(window.matchMedia(media).matches) { | |
$('.element').attr('placeholder', placeholderLong); | |
} | |
else { | |
$('.element').attr('placeholder', placeholderShort); | |
} | |
}); | |
// Slightly not nice way using width | |
$(window).resize(function(){ | |
var cutoff = 720, | |
placeholderShort = "Short holder", | |
placeholderLong = "My quite long placeholder"; | |
if($(window).width() >= cutoff) { | |
$('.element').attr('placeholder', placeholderLong); | |
} | |
else { | |
$('.element').attr('placeholder', placeholderShort); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment