Stephen Murphy![]()
Cross browser/device text selection events.
- selection: Fired when text has been selected.
- selectionchange: Fired when text has been selected or deselected.
Fired events have the following properties:
- selection: Selected text.
- pageX/pageY: Best guess on where selection ends.
Limitations:
- There are a few edge cases where selection events don't work well. Namely, when selecting text that crosses the boundary of a bounded node or selecting text with only keyboard selection.
Notes:
- Polling for selection changes is necessary because iOS doesn't tell us when the selection region has been updated and desktop browsers can use keyboard selection.
- iOS requires a slight delay when getting selected text.
<script src="http://yui.yahooapis.com/3.6.0pr2/build/yui/yui-min.js"></script>YUI({
//Last Gallery Build of this module
gallery: 'gallery-2012.10.03-20-02'
}).use('gallery-event-selection', function(Y) {
// Fired on text selection/deselection
Y.one('body').on('selectionchange', function(e) {
console.log('selectionchange: ' + e.selection);
});
// Fired only on text selection
Y.one('body div').on('selection', function(e) {
console.log('selection: ' + e.selection);
});
});
© 2006-2013 Yahoo! Inc. All rights reserved.
All code on this site is licensed under the BSD License unless stated otherwise.
About This Site · Security Contact Info