I am currently working on a JavaScript bookmarklet that will function as a highlighter, changing the background color of selected text on a webpage to yellow when the bookmarklet is clicked.
The code I have for retrieving the selected text is functioning correctly and returning the desired string:
function getSelectedText() {
var selection = '';
if (window.getSelection) {
selection = window.getSelection();
} else if (document.getSelection) {
selection = document.getSelection();
} else if (document.selection) {
selection = document.selection.createRange().text;
}
return selection;
}
However, I am facing issues with a similar function that aims to change the CSS properties of the selected text using jQuery:
function applyHighlightToSelectedText() {
var selectedText;
if (window.getSelection) {
selectedText = window.getSelection();
} else if (document.getSelection) {
selectedText = document.getSelection();
} else if (document.selection) {
selectedText = document.selection.createRange().text;
}
$(selectedText).css({'background-color' : 'yellow', 'font-weight' : 'bolder'});
}
Any suggestions on how to resolve this issue?