Skip to content Skip to sidebar Skip to footer

Clicking Inside Canvas Element Selects Text

I have a canvas element in my HTML document. When I click inside of the canvas multiple times, it selects part of my

element's text, which is before the t

Solution 1:

Returning false in an event stops the standard event from happening:

document.getElementById('canvas').onmousedown = function(){
  returnfalse;
};

Edit: I just found out that text selection is done before onclick is fired, a better option is onmousedown.

Solution 2:

If you want to use the mousedown event to do other things, you can prevent only text selection more specifically by setting the onselectstart event to return false.

//give your canvas an id, I used 'can'    var canvas = document.getElementById('can');
canvas.onselectstart = function () { returnfalse; }

Post a Comment for "Clicking Inside Canvas Element Selects Text"