How can I prevent text selection on Mousedown event?

I'm having trouble in dealing with mouse events, because when I double-click on span element it selects text but thats not what I want. Here is my simple program: <div>Learning mouse events is fun <span onclick="alert('Clicked')"> Click here. </span> for more. </div> I want that, When I click on that element, work as button not a text, briefly I want to disable selection. Show me best way!

»Javascript @slimtevez

» 1 answer  » 19 views   

Add photos

Gadrawin • 1 month ago
When you press without releasing the left mouse button, that action will result to text selection and most of times that behavior is contrary to your needs because it adds unnecessary event (select). But here there is a good way to prevent that action/event(select) from occurring, Solution: prevent the browser action on mousedown! I have simple example: Left Text <span style="color: red" ondblclick="alert('Double-Clicked!')" onmousedown="return false">Middle Text </span> Right Text Cool! You can select both Left text and Right text but you can't do selection on Middle text since we return false on onmousedown attribute. thats it. Remember, If you start your selection from left or right that middle element is still selectable, But you can't select it by starting from itself. »

0  Dislike  •  Reply

Back to top

[SHARE THIS POST]: The post's archived Spread it to others!

Posted under this donnektish »
This community is about sharing contents such as questions and answers discussing about Javascript and TypeScript, ES5, ES6
58 people joined this donnektish

Maintain social distancing, to avoid Covid19