window.getSelection() for contenteditable div *on click*
Asked Answered
H

2

8

I have a contenteditable div and want to get the user's selection when they click a span.

My problem is that when I click the span, the selection gets unselected so window.getSelection().toString() returns ''.

How can I get this to work on click of a span?

I know the actual getSelection() works, because if I wrap window.getSelection().toString() in a setTimeout of 5 seconds, after 5 seconds, I get the selected text!

My code:

$('#btn').click(function() {
  console.log(window.getSelection().toString()); //returns ''
});
#btn {
  cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id='btn'>get selection</span>
<br><br>
<div id='ce' contenteditable='true'>test</div>
Heaver answered 23/8, 2015 at 18:56 Comment(1)
get selection text on mouse up of div id="ce" #5379620Whipple
R
1

Since there is no event you can use to specifically detect a 'select' or 'deselect', you'll have to listen to a mouseup event and populate a "cache variable" that can store the selection in the memory:

var selection = '';
document.getElementById('ce').onmouseup = function(){
  selection = window.getSelection().toString();
};

document.getElementById('btn').onclick = function(){
  console.log(selection);
};

Or, provided you have jQuery, you could try this more complaint version, which also factors in keyboard-based selections:

var selection = '', shifted = false;
$('#ce').on('mouseup keyup keydown', function(e){
  if (e.type === 'keydown') {
    shifted = e.shiftKey;
    return;
  }

  if (
    e.type === 'mouseup' ||
    (shifted && (e.keyCode === 39 || 37 || 38 || 40))
  ){
    selection = window.getSelection().toString();
  }
});

$('#btn').on('click', function(){
  console.log(selection);
});
Rozalin answered 23/8, 2015 at 19:8 Comment(0)
A
2

You can store the selection when there is a click on your contenteditable div, then return it when you click on the button.

document.querySelector("#ce").addEventListener(function(){
  userSelection= window.getSelection().toString();
});


document.querySelector("#btn").addEventListener("mouseup",function(){

  document.querySelector("#selection").innerHTML= 
    "You have selected:<br/><span class='selection'>" +  userSelection +"</span>";

});

http://jsfiddle.net/xnvp38u3/

Association answered 23/8, 2015 at 19:44 Comment(0)
R
1

Since there is no event you can use to specifically detect a 'select' or 'deselect', you'll have to listen to a mouseup event and populate a "cache variable" that can store the selection in the memory:

var selection = '';
document.getElementById('ce').onmouseup = function(){
  selection = window.getSelection().toString();
};

document.getElementById('btn').onclick = function(){
  console.log(selection);
};

Or, provided you have jQuery, you could try this more complaint version, which also factors in keyboard-based selections:

var selection = '', shifted = false;
$('#ce').on('mouseup keyup keydown', function(e){
  if (e.type === 'keydown') {
    shifted = e.shiftKey;
    return;
  }

  if (
    e.type === 'mouseup' ||
    (shifted && (e.keyCode === 39 || 37 || 38 || 40))
  ){
    selection = window.getSelection().toString();
  }
});

$('#btn').on('click', function(){
  console.log(selection);
});
Rozalin answered 23/8, 2015 at 19:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.