Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double click word JavaScript window.getSelection in two INS tag

My HTML is:

<p><ins data-id="1">111</ins><ins data-id="2">222</ins></p>

The output of this code is:

enter image description here

If I double click the word, it's selecting the complete word, like this:

enter image description here

But I want to select the letters based on the INS tag data-id

Ex:- if I double click the 111 I want to select only 111 like this:

enter image description here

How to modify the default double click selection to JavaScript selection?

I tried the following code:

var containerid = $(e.currentTarget);
if (window.getSelection) {
   var range = document.createRange();
   range.selectNode(containerid);
   var sel = window.getSelection()
   sel.removeAllRanges();
   sel.addRange(range);
}

But it's not working as expected.

like image 392
Dhanush Bala Avatar asked Nov 10 '16 09:11

Dhanush Bala


1 Answers

<p><ins data-id="1">111</ins>&#8203;<ins data-id="2">222</ins></p>

You could just put a zero-width space between them: &#8203;

like image 113
Oliver Salzburg Avatar answered Oct 02 '22 14:10

Oliver Salzburg