Get Selected/Highlighted text using JQuery


In this article, we will be going through a simple Javascript and JQuery code that enables us to get the selected or highlighted text.

In case, you want to show a custom menu or enable user to share the text via social medias or copy the selected text, this would be pretty useful.

Source Code

function getSelectionText() {
    var text = "";
    if (window.getSelection) {
        text = window.getSelection().toString();
    } else if (document.selection && document.selection.type != "Control") {
        text = document.selection.createRange().text;
    return text;

$(document).ready(function() {
    $(document).bind("mouseup", function() {
        var selectedText = getSelectionText();
        if(selectedText != ''){


ALSO READ  Creating an Audio Recorder with JavaScript: A Step-by-Step Guide

Comments are closed.