Skip to content

Instantly share code, notes, and snippets.

@mponizil
Created March 24, 2014 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mponizil/9744182 to your computer and use it in GitHub Desktop.
Save mponizil/9744182 to your computer and use it in GitHub Desktop.
jquery-placeholder.coffee
define [
'jquery'
], ($) ->
$.support.placeholder = 'placeholder' of document.createElement('input')
placeholder =
listen: (container) ->
container.find('[placeholder]').each (i, el) ->
$el = $(el)
value = $el.attr('placeholder')
do ($el, value) ->
$el.val(value) if not $el.val()
$el.on 'focus', -> $el.val('') if $el.val() is value
$el.on 'blur', -> $el.val(value) if not $el.val()
serialize: (container) ->
container.find('[placeholder]').each (i, el) ->
$el = $(el)
$el.val('') if $el.val() is $el.attr('placeholder')
$.fn.placeholder = (fn='listen') ->
if $.support.placeholder
return this
placeholder[fn](this)
return this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment