Not sure if the title is well chosen...
I am trying to simulate text-selection in HTML/JS/CSS to get rid of the action bubble on mobile device when truly selecting texts.
To be more specific, I'm trying to avoid this:
The visual:
The way I built it and it may change because it doesn't matter, is that the text selected is wrapped inside a span.selection
and inside that tag, there are also two caret used as handlers:
Lorem ipsum dolor <!-- Unselected Text -->
<span class="selection"> <!-- Start selection wrapper -->
<span rel="previous" class="caret"></span> <!-- The left-side caret -->
sit amet, consectetur <!-- The selected texts -->
<span rel="next" class="caret"></span> <!-- The right-side caret -->
</span> <!-- End selection wrapper -->
adipiscing elit. <!-- Unselected Text -->
Ideally, it would be fun to use a drag-n-drop to select more or less texts but I believe this would be darn hard to do and in that case, maybe using click on the carets to select either the previous or the following word and wrap it inside the .selection
wouldn't be that bad.
Here is the jsfiddle: http://jsfiddle.net/m6Qx4/
The surrounding texts may contains HTML tags too such as: <i>
, <b>
, <span>
and <ul>/<li>
may be present making the parsing harder.
Any ideas how this can be done?
Status Update:
I have actually managed to make it work with .click();
event listener using my custom jQuery methods.
Eventually, I will replace the click events with jQuery UI draggable to select surrounding words so long as it's useable for mobile devices.
My current bug consists of the re-position of the red carets. I have tried destroying them and prepend/append them back and it's still not working. It might be a bug with Firefox that can't reload the DOM properly after changes made to Text Nodes?
Reference: jsFiddle
To check operating condition of jsFiddle.net due to recent outages, visit their Tweets.
console.info
in your jsFiddle, you may need to use variablelastWordArray
instead oflastWord
since that was a previous correction. Not to sure what needs to be changed. – Larynx