How to get the Highlighted/Selected text in JavaScript? Last Updated : 19 Apr, 2023 Comments Improve Suggest changes Like Article Like Report There may be a need to find out the text selected/highlighted by the user. It can be done very easily using the window and document objects and their properties. Handling selected text is different for different browsers. The ways to get selected text are shown below: Method 1: window.getSelection property: The window.getSelection method returns a selection object that represents the text selection made by the user or the caret's current position. Syntax: function selection(){ if (window.getSelection) return window.getSelection(); } Method 2: document.getSelection property: The method of document interface returns a selection object that represents the user-selected text. Syntax: function selection(){ if (document.getSelection) return document.getSelection(); } Method 3: document.selection property: This method also returns a selection object that displays the text selected by the user. Syntax: function selection(){ if (document.selection) return document.selection.createRange().text;; } Time to try out the code. Run the code, select a text, and press the button to show the selected text: Example: In this example, we will use all three methods to get highlighted or selected text. HTML <h1 style=color:green> GeeksforGeeks </h1> <p>Select any part of this sentence and press the button</p> <!--Button to invoke the function to get the selected text--> <input type="button" value="Get Selection" onmousedown="getSelectedText()"> <!--Form to show the selected text as output--> <form name="testform"> <textarea name="selectedtext" rows="5" cols="20"> </textarea> </form> <script> // Function to get the Selected Text function getSelectedText() { var selectedText = ''; // window.getSelection if (window.getSelection) { selectedText = window.getSelection(); } // document.getSelection else if (document.getSelection) { selectedText = document.getSelection(); } // document.selection else if (document.selection) { selectedText = document.selection.createRange().text; } else return; // To write the selected text into the textarea document.testform.selectedtext.value = selectedText; } </script> Output: How to get the Highlighted/Selected text in JavaScript? Comment More infoAdvertise with us Next Article How to get the Highlighted/Selected text in JavaScript? N Nilarjun Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to Highlight the Searched String Result using JavaScript ? Given below is an HTML document which is basically about how to highlight the searched string result. In this article, we are using HTML, CSS, JavaScript, Bootstrap and mark.js to make our website more effective. Moreover, exclusively for highlighting the searched string among a given context or par 4 min read How to add Text Highlighter in Next.js ? In this article, we are going to learn how we can add Text Highlighter in NextJs. NextJS is a React-based framework. It has the power to Develop beautiful Web applications for different platforms like Windows, Linux, and mac. The linking of dynamic paths helps in rendering your NextJS components con 2 min read How to select all Text in HTML Text Input when clicked using JavaScript? To select the text in HTML Text Input we can use the JavaScript DOM methods and we will select the text when we click the text box.Using document.getElementById() methodSyntax:<input onClick="this.select();" > or document.getElementById("ID").select();Example 1: We will use "this.select()" in 1 min read How to select DOM Elements in JavaScript ? Selecting DOM (Document Object Model) elements is a fundamental aspect of web development with JavaScript. It allows developers to interact with and manipulate elements on a webpage dynamically. Proper selection of elements is crucial for tasks such as updating content, adding event listeners, or mo 3 min read How to Copy the Text to the Clipboard in JavaScript? The document.execCommand("copy") method is commonly used to Copy the Text to the Clipboard, allowing developers to copy text programmatically, making it available for pasting elsewhere. To copy text from an input box using JavaScript, first use 'document.getElementById()' to access the input element 1 min read How to load the contents of a text file into a JavaScript variable? In this article, we will examine how to read the contents of any text file that exists on your computer into a variable using JavaScript. The following are a few basic pointers that everybody should brush through before looking at the code: Event listeners: These are predefined functions that exist 3 min read How to highlight text based on user input with React.js ? One common feature in many applications is the ability to highlight specific portions of text based on user input. In this article, we will explore how to implement text highlighting functionality using React.js. The following approach covers how to highlight text input given by users in ReactJS. It 2 min read How to Select an Element by ID in JavaScript ? In JavaScript, we can use the "id" attribute to interact with HTML elements. The "id" attribute in HTML assigns a unique identifier to an element. This uniqueness makes it easy for JavaScript to precisely target and manipulate specific elements on a webpage. Selecting elements by ID helps in dynamic 2 min read How to Get Selected Index on Mouseover in JavaScript ? To obtain the selected index on mouseover in JavaScript, attach event listeners to the parent container and utilize iteration through child elements to discover the index of the hovered element. By tracking the index of the hovered element during this process, you can determine the selected index ac 2 min read How to Disable Text Selection in ReactJS ? In this article, we will see how to disable the text selection in ReactJS. Prerequisites:Introduction to ReactReact HooksNPM or NPXWeb applications ofteÂn provide a valuable feature called text seleÂction, enabling users to easily highlight and copy content from a webpage. However, there are situat 4 min read Like