Skilled in SEO, content writing, and digital marketing. Completed several years of working in many organizations including multinational companies. I love to learn new things in life that keep me motivated.
To prevent selection of text when using Shift+Click, you can use JavaScript to disable text selection on the
mousedown event when the Shift key is pressed. Here's an example:
// Select the element you want to make unselectable
var element = document.getElementById("my-element");
// Add an event listener to the element
element.addEventListener("mousedown", function(event) {
// Check if the Shift key is pressed
if (event.shiftKey) {
// Disable text selection
event.preventDefault();
}
});
In this example, we're checking if the Shift key is pressed when the user clicks on the element using the
shiftKey property of the event object. If the Shift key is pressed, we're calling the
preventDefault() method to prevent text selection.
Note that this technique only prevents text selection when using Shift+Click. Users can still select the text using other methods, such as dragging the mouse over the text or using the keyboard.
Liked By
Write Answer
Prevent selection from Shift+Click
Join MindStick Community
You have need login or register for voting of answers or question.
Aryan Kumar
27-Apr-2023To prevent selection of text when using Shift+Click, you can use JavaScript to disable text selection on the mousedown event when the Shift key is pressed. Here's an example:
In this example, we're checking if the Shift key is pressed when the user clicks on the element using the shiftKey property of the event object. If the Shift key is pressed, we're calling the preventDefault() method to prevent text selection.
Note that this technique only prevents text selection when using Shift+Click. Users can still select the text using other methods, such as dragging the mouse over the text or using the keyboard.