Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

disable text selection on double click

I have script that toggles menu from the side:

<script>
    // Closes the sidebar menu
    $("#menu-close").click(function (e) {
        e.preventDefault();
        $("#sidebar-wrapper").toggleClass("active");
    });
    // Opens the sidebar menu
    $("#menu-toggle").click(function (e) {
        e.preventDefault();
        $("#sidebar-wrapper").toggleClass("active");
    });
</script>

and I use this in every section tag:

ondblclick = $("#menu-close").click()

but the problem is that when I double click somewhere it also selects text. The question is: How can I disable this double click selection on the whole page? It'd be cool if I could implement it into my actual script.

UPDATE: now I have

document.getElementById("el").ondbclick = function(ev) {
    ev.preventDefault()
    alert()
    return false}

$("#menu-close").click(function (e) {
    e.preventDefault();
    $("#sidebar-wrapper").toggleClass("active");
});
// Opens the sidebar menu
$("#menu-toggle").click(function (e) {
    e.preventDefault();
    $("#sidebar-wrapper").toggleClass("active");
});

and id="el" on elements I don't want to be selected.. but it still doesnt work

like image 742
Yamiteru Iori Avatar asked Dec 26 '15 11:12

Yamiteru Iori


People also ask

How do I turn off double click highlighting?

To disable the text selection when the user double clicks on the HTML element, you can use the user-select property and set its value to none in CSS.

How can I prevent text element selection with cursor drag?

You'll need to call event. preventDefault() in both the down and move events in order to keep the browser from selecting text. Save this answer.


2 Answers

(element).querySelector('mousedown', (e) => {
    e.preventDefault()
})

In Plain JavaScript...

like image 174
Abishek H Avatar answered Oct 19 '22 18:10

Abishek H


Use these CSS-Rules on the Element:

#menu-close {
    -moz-user-select: none;
    -webkit-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

Do this on every Element that should not be selected. Or just on body { for everything.


You could also remove the double-click selection after it happened, if you don't want to disable selection everywhere:

document.getElementById("el").ondbclick = function(ev) {
  ev.preventDefault()
  alert()
  return false
}
<div id="el"> asd</div>
like image 41
CoderPi Avatar answered Oct 19 '22 18:10

CoderPi