Skip to content

Instantly share code, notes, and snippets.

@horaciod
Created April 25, 2015 14:35
Show Gist options
  • Save horaciod/47bdb231e9208074675d to your computer and use it in GitHub Desktop.
Save horaciod/47bdb231e9208074675d to your computer and use it in GitHub Desktop.
boton borrado de campos bootstrap
$ = jQuery
$.fn.inputClear = (options={}) ->
@each (i, input) ->
input = $(input)
clearButton = input.data("clear-button")
init = ->
icon = options.icon ? "glyphicon-remove"
iconHtml = if icon
"""<span class="glyphicon #{icon}"></span>"""
else
""
html = options.html || ""
html = " #{html}" if icon && options.html
clearButton = $("""<a href="#">#{iconHtml}#{html}</a>""")
.css
display: "inline"
cursor: "pointer"
color: "#888"
position: "absolute"
top: input.position().top
left: input.position().left + input.outerWidth()
margin: "8px 0px 0px -20px"
.on "click", (e) ->
e.preventDefault()
input
.val("")
.trigger "change"
.focus()
showHideButton()
input.data("clear-button", clearButton)
.on("keyup", showHideButton)
.after(clearButton)
showHideButton()
showHideButton = ->
clearButton.toggle(input.val() != "")
init() unless clearButton?
this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment