Looking for a method to target selected text and perform an action on it in Javascript. What kind of methods should I be using? Is this a job for jQuery? Thanks so much!
EDIT: Earlier answers regarded targeting a CSS class. I’m looking for clicking and highlighting/selecting text and then acting on that in JS. Thanks!
EDIT 2: I’ve been asked for an example of this function. Here’s one but the code has poor reviews. Let me know what you think.
It is quite difficult to write a cross browser, generic “get selected text” function. If you can limit your requirements to say only text selected in a page, then life is simpler.
But if you want to be able to get text selections from anywhere (inside form controls, button labels, general text), then life is tough.
Here’s a function I wrote some time ago, it worked well enough for the application it was used in: