Skip to content

Instantly share code, notes, and snippets.

@norcal82
Created June 18, 2015 16:10
Show Gist options
  • Save norcal82/5a30b096d2b6c939b55d to your computer and use it in GitHub Desktop.
Save norcal82/5a30b096d2b6c939b55d to your computer and use it in GitHub Desktop.
mini data binding
$('body')
.on 'focus', '[contenteditable]', ->
$this = $(this)
$this.data 'before', $this.html()
.on 'blur keyup paste input', '[contenteditable]', ->
$this = $(this)
if $this.data('before') isnt $this.html()
$this.data 'before', $this.html()
$this.trigger('change')
new_text = $this[0].innerText
# hacky. will bind form text and element text
$('.'+$this.data('bind-id')).val(new_text).text(new_text)
// place data-input-id attr on element with a class that targets elements you want to bind to
// this will bind the contenteditable data to the input val
%h1{contenteditable: "", :"data-bind-id" => "name-bind"} My Name
%input{:class => "name-bind"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment