Selecting text behind another element with createEvent
Asked Answered
F

2

13

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.

Feliciafeliciano answered 4/1, 2012 at 12:17 Comment(0)
S
7

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
Schmaltz answered 9/1, 2012 at 15:18 Comment(0)
O
2

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.

Osteoblast answered 9/1, 2012 at 14:53 Comment(1)
Technically, createEvent should be perfectly cross-browser and I don't see how styling could affect the native browser behavior, which should be identical to both native and synthetic events.Feliciafeliciano

© 2022 - 2024 — McMap. All rights reserved.