In JavaScript, I don't know how to use window.getSelection()
with a single click?
var s = window.getSelection();
s = s.toString().trim();
alert(s);
HTML contains:
<p>This text contains --this; and that-that.</p>
If single click on --this; the expected output should be this. Double click should do this well, but how to do this with just a single click?
Thank you very much to all, I come to this solution:
$('p').bind('click', function () {
var sel_obj = window.getSelection(); //it will return an object
sel_obj.modify("move","forward","character");
sel_obj.modify("extend","backward","word");
sel_obj.modify("move","backward","character");
sel_obj.modify("extend","forward","word");
var text = sel_obj.toString().trim();
text = text.toLowerCase();
alert (text);
References: https://developer.mozilla.org/en-US/docs/Web/API/Selection
https://developer.mozilla.org/en-US/docs/Web/API/Selection/modify