JavaScript, Browser

Gets the currently selected text.

  • Use Window.getSelection() and Selection.toString() to get the currently selected text.
const getSelectedText = () => window.getSelection().toString();
getSelectedText(); // 'Lorem ipsum'

Recommended snippets

  • getURLParameters

    JavaScript, Browser

    Creates an object containing the parameters of the current URL.

  • getParentsUntil

    JavaScript, Browser

    Finds all the ancestors of an element up until the element matched by the specified selector.

  • getScrollPosition

    JavaScript, Browser

    Returns the scroll position of the current page.