Skip to content

Instantly share code, notes, and snippets.

@DVG
Created June 15, 2012 19:03
Show Gist options
  • Save DVG/2938205 to your computer and use it in GitHub Desktop.
Save DVG/2938205 to your computer and use it in GitHub Desktop.
Jquery wrapText Fucntion in Coffeescript
wrapText = (elementID, openTag, closeTag) ->
textArea = $("##{elementID}")
len = textArea.val().length
start = textArea[0].selectionStart()
end = textArea[0].selectionEnd()
selectedText = textArea.val().substring(start, end)
replacement = "#{openTag}#{selectedText}#{closeTag}"
textArea.val(textArea.val().substring(0, start) + replacement + textArea.val().substring(end, len))
#example click function
$('#bold_button').click (event) ->
wrapText('post_body', '[b]', '[/b]')
event.preventDefault()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment