Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting text behind another element with createEvent

I have a scenario where I have some text, which should be user-selectable. The problem is, that there's an UI overlay on top of it, which prevents selecting text by default. The logical way to keep the overlay and still be able to select the text, would be to use synthetic events (use document.createEvent), but due to some reason, it doesn't work as expected.

The events seem to be delegated correctly and fire their handlers, but no text is selected. I have an example here, which is a rough simplification of the problem.

A few notes

  1. In Firefox if you start your selection outside of the overlay, you are still able to select the text you want, even if it's under the overlay
  2. When you have a normal selection in the uncovered area and you click on the overlay, it would be expected from the delegated mousedown event to remove the selection, but it doesn't happen

Am I missing an event that should also be delegated (I have mousedown, mousemove and mouseup)? Or is it some kind of a security measure by browsers to disable such behavior (refer to the note nr 2)? Any other suggestions on how to get the desired result? I know I should work around the current overlay solution altogether, but I'm already curious about the problem itself.

like image 348
zatatatata Avatar asked Jan 04 '12 12:01

zatatatata


2 Answers

I have found two solutions for this problem:

  1. "pointer-events" css property. Requires IE 9.0+ though.
  2. Seems like guys from ExtJS solved it by event forwarding: demo, source, blog post
like image 103
ionoy Avatar answered Nov 08 '22 07:11

ionoy


I would suggest to do the easy trick: put the transparent element with the same content as your text on-top of text itself and overlay. Here is the demonstration.

P.S.: From my experience any solution in the form you suggest will be awful. It will suffer from browser incompatibilities, side-effects of surrounding mark-up and styling etc.

like image 25
bjornd Avatar answered Nov 08 '22 08:11

bjornd