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.
js code to highlight the text. There are many built-in functions in mark. js but we are using two functions for our requirement that is mark() and unmark() function respectively. Here mark() is used to highlight the search text and unmark() is used to remove highlighting the text that is highlighted before.
Highlight Text Using the Mark Tag Method in JavaScript Another method that you can use to highlight the text is the mark tag. If you surround any text inside the mark tag, the browser will automatically highlight it in a striking yellow color.
The HTML <mark> tag is used to mark or highlight text that is of special interest or relevance in an HTML document. Browsers traditionally render the text found within the <mark> tag as text with a yellow background color.
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:
/*
* This function returns the selected text in a document.
* If no text selected, returns an empty string.
*
* Call on one of the following events:
*
* mouseup - most appropriate event
* for selection by mousedown, drag to select, mouseup
* may select only whitespace
*
* dblclick - not as appropriate as mouseup
* for selection of word by double click
* may select only whitespace
*
* Note that text can be selected in ways that do not dispatch
* an event, e.g. selecting all the text in the document using:
* ctrl + a
* context menu -> Select All
* edit menu -> Select All
* programmatically selecting text
*/
function checkForSelectedText(e) {
var e = e || window.event;
var el = e.target || e.srcElement;
var tagName = el.tagName && el.tagName.toLowerCase();
var t;
var d = document;
// Try DOM 2 Range - for most browsers, including IE 6+
// However, doesn't get text selected inside form controls
// that allow selection of text content (input type text,
// textarea)
if (d && d.selection && d.selection.createRange) {
t = d.selection.createRange().text;
// Otherwise try HTML5 - note that getSelection returns
// a string with extra properties. This may also get
// text within input and textarea
} else if (d.getSelection) {
t = d.getSelection();
}
// If didn't get any text, see if event was inside
// inupt@type=text or textarea and look for text
if (t == '') {
if (tagName == 'textarea' ||
(tagName == 'input' && el.type == 'text')) {
// Check selectionStart/End as otherwise if no text
// selected, IE returns entire text of element
if (typeof el.selectionStart == 'number' &&
el.selectionStart != el.selectionEnd) {
t = el.value.substring(el.selectionStart, el.selectionEnd)
}
}
}
return t;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With