Skip to content

Instantly share code, notes, and snippets.

@alanhogan
Created August 26, 2014 00:39
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 alanhogan/1657b3a6c8e58079a4a2 to your computer and use it in GitHub Desktop.
Save alanhogan/1657b3a6c8e58079a4a2 to your computer and use it in GitHub Desktop.
CoffeeScript Require.js Module for jQuery-based element selection on click
$ = require 'jquery'
module.exports = do ->
init = ->
$(document).on 'click', '.js-autoselect', (e) ->
range = document.createRange()
selection = window.getSelection()
range.selectNodeContents this
selection.removeAllRanges()
selection.addRange range
return
{
init: init
}
@alanhogan
Copy link
Author

IE9+, I believe, and of course Chrome, Safari, Firefox are supported
Note this does not work on at least some mobile browsers, including Mobile Safari (as of iOS 7.0) as they seem to not support programmatic selection of non-editable text (including <input readonly>)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment