Disable Text Selection for Particular Elements

In this approach, we will use react.js to disable only specific text not all text. the functionality is achieved by e­mploying the useEffect hook. Upon execution, it targets all e­lements tagged with the­ “no-select” class and applies styles to them, specifically manipulating prope­rties like webkitUse­rSelect, mozUserSe­lect, msUserSele­ct, and userSelect. By se­tting these propertie­s to “none,” users are e­ffectively preve­nted from selecting or highlighting any te­xt within those eleme­nts. 

Example: In this example we are using the above-explained approach.

Javascript




// App.js file
 
import React, { useEffect } from "react";
 
const App = () => {
    useEffect(() => {
 
        // Disable text selection for elements
        // with class "no-select"
        const noSelectElements =
            document.querySelectorAll(".no-select");
        noSelectElements.forEach((element) => {
            element.style.webkitUserSelect = "none";
            element.style.mozUserSelect = "none";
            element.style.msUserSelect = "none";
            element.style.userSelect = "none";
        });
    }, []);
 
    return (
        <div style={styles.body}>
            <div style={styles.container}>
                <h1>Disable Text Selection Using React</h1>
                <div
                    className="no-select"
                    style={styles.noSelect}
                >
                    <h1>Welcome To w3wiki</h1>
                    <p>This text is not selectable</p>
                </div>
                <p style={styles.p}>
                    A Computer Science portal for geeks. It
                    contains well-written, well-thought, and
                    well-explained computer science and
                    programming articles.
                </p>
            </div>
        </div>
    );
};
 
export default App;
 
const styles = {
    body: {
        fontFamily: "Arial, sans-serif",
        margin: 0,
        padding: 0,
    },
    container: {
        maxWidth: "800px",
        margin: "0 auto",
        padding: "20px",
        backgroundColor: "#fff",
        border: "1px solid #ccc",
        borderRadius: "5px",
        boxShadow: "0 0px 10px 0px black",
        margin: "4rem",
    },
    noSelect: {
        border: "3px solid green",
        padding: "20px",
        backgroundColor: "#f9f9f9",
        borderRadius: "10px",
        marginBottom: "10px",
        color: "green",
    },
    p: {
        lineHeight: "1.6",
    },
};


How to Disable Text Selection in ReactJS ?

In this article, we will see how to disable the text selection in ReactJS.

Similar Reads

Prerequisites:

Introduction to React React Hooks NPM or NPX...

Steps to Create React Application:

Step 1: Create a react application by using this command...

Approach 1: Disable Text Selection for Particular Elements

In this approach, we will use react.js to disable only specific text not all text. the functionality is achieved by e­mploying the useEffect hook. Upon execution, it targets all e­lements tagged with the­ “no-select” class and applies styles to them, specifically manipulating prope­rties like webkitUse­rSelect, mozUserSe­lect, msUserSele­ct, and userSelect. By se­tting these propertie­s to “none,” users are e­ffectively preve­nted from selecting or highlighting any te­xt within those eleme­nts. ...

Steps to run:

...

Approach 2: Toggle Text Selection On and Off

To open the application, use the Terminal and enter the command listed below....

Contact Us