
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to set whether the text of an element can be selected or not with JavaScript?
In this tutorial, we will learn to set whether the text of an element can be selected or not with JavaScript. Use the userSelect property in JavaScript to enable or disable a text selection. For Firefox, use the MozUserSelect property and set it to none, to disable the selection.
The CSS user-select property can set the text on a web page as selectable or nonselectable. But, sometimes, we must restrict the selection to a triggered event or a condition. Then, we can use the JavaScript DOM that provides nearly all CSS properties.
So, let us look at how to set whether the text of an element can be selected or not.
Using the userSelect Property
The userSelect property of JavaScript DOM is used to set the text of an element as selectable or non-selectable. We need to double-click on a text to select it. At the same time, this property can also set the text to select with only one click.
We can follow the below syntax to use the userSelect property to set if an element's text can be selected or not using JavaScript.
Syntax
var element = document.getElementById(" <your ID here> "); element.style.userSelect = "auto || none || text || all";
Parameters
auto − It's a default value. You can select the text.
none − Set the text to non-selectable.
text − Set the text selectable.
all − Set the text selectable with only one click.
Example 1
You can try to run the following code to set whether the text of an element can be selected or not with JavaScript −
<!DOCTYPE html> <html> <body> <button onclick = "myFunction()">Click me</button> <div id = "box"> Click the above button. This won't allow you to select this text. Shows the usage of userSelect property. </div> <script> function myFunction() { var a = document.getElementById("box"); a.style.userSelect = "none"; // Works in Chrome and Safari a.style.WebkitUserSelect = "none"; // Works in Firefox a.style.MozUserSelect = "none"; } </script> </body> </html>
Example 2
In the example, we have added a paragraph inside a div container. The text in the paragraph will not be selectable if the button is clicked.
<html> <head> <style> body{ margin: 1%; } #div{ padding: 2%; border: 1px solid red; width: 500px; text-align: center; margin: 5px; } #btn{ font-size: larger; background-color: black; color: white; margin: 5px; } </style> </head> <body> <h3>Use <i>userSelect property</i> to set whether the text of an element can be selected or not</h3> <div id = "div">This is a div <p id = "para">Lorem ipsum dolor sit amet consectetur adipisicing elit. Obcaecati, nesciunt. Ullam corporis eaque culpa, corrupti earum aut perferendis rerum sequi!</p> </div> <button id = "btn"> Apply </button> <script> document.getElementById("btn").addEventListener("click", Apply); function Apply(){ document.getElementById("para").style.userSelect="none"; document.getElementById("para").style.color="#949494"; } </script> </body> </html>
In the above example, users can see that we have used the userSelect property to set the text in the paragraph non-selectable on a button after clicking it.
Example 3
In the example below, we have added four radio buttons that are the values of the userSelect Property. After selecting any radio button, the userSelect property will be applied to the text in the div container.
<html> <head> <style> #container{ padding: 2%; border: 2px dotted purple; width: 500; text-align: center; margin: 5px; } </style> </head> <body> <h3>Use <i>userSelect</i> property to set whether the text of an element can be selected or not</h3> <div id = "container">Lorem ipsum dolor sit amet consectetur adipisicing elit. Unde, voluptatem velit! Magni eligendi quibusdam distinctio, temporibus ad beatae? Quas, reiciendis.</div> <br> Apply userSelect with one of the following values and try to select the above text: <br> <input type = "radio" name = "radio_btn" id = "Auto" value = "auto" onchange = "selected()"/> <label for = "Auto"> auto </label> <br> <input type = "radio" name = "radio_btn" id = "None" value = "none" onchange = "selected()"/> <label for = "None"> none </label> <br> <input type = "radio" name = "radio_btn" id = "Text" value = "text" onchange = "selected()"/> <label for = "Text"> text </label> <br> <input type = "radio" name = "radio_btn" id = "All" value = "all" onchange = "selected()"/> <label for = "All"> all </label> <br> <script> var element = document.getElementById("container"); function selected(){ var radio_selected = document.querySelector( 'input[name="radio_btn"]:checked'); element.style.userSelect = radio_selected.value; if(radio_selected.value == "none"){ element.style.color = "#949494"; } } </script> </body> </html>
In the above example, users can see that we have applied the "all" property value for userSelect. This helps us in selecting a text. We can select a text using only one click with the "all" value of the property.
In this tutorial, we have learned to set whether the text of an element can be selected or not with JavaScript.
- Related Articles
- Set whether the text of the element can be selected or not with CSS
- Set whether or not an element should be visible while not facing the screen with JavaScript?
- How to set whether or not an element is resizable by the user with JavaScript?
- How to set whether an element is draggable or not in HTML?
- How to set whether an element should be visible in JavaScript?
- Set whether the column in the table model can be selected or deselected in Java?
- Set whether the row in the table model can be selected or deselected in Java?
- Set whether the cell in the table model can be selected or deselected in Java?
- How to specify whether the content of an element should be translated or not in HTML?
- How to set or return the number of columns an element should be divided into with JavaScript?
- Set whether the flexible items should wrap or not with JavaScript?
- Set whether the table border should be collapsed into a single border or not with JavaScript?
- How to set whether the image-border should be repeated, rounded or stretched with JavaScript?
- How to set the padding of an element with JavaScript?
- How to set the height of an element with JavaScript?
