disable text selection and add exceptions
Asked Answered
S

2

1

if i use this style for the <body>

onmousedown='return false;' onselectstart='return false;'

is it possible to give the exceptions by using javascript for inputs textareas and selects? and if yes then can u show me how?

Septuplicate answered 17/1, 2011 at 10:17 Comment(0)
P
2

Actually, you can disable text selection without needing heavy-handed JavaScript event handlers. See my answer here: How to disable text selection highlighting using CSS?

Summary: use CSS and the unselectable expando in IE.

CSS:

*.unselectable {
   -moz-user-select: -moz-none;
   -khtml-user-select: none;
   -webkit-user-select: none;
   -o-user-select: none;
   user-select: none;
}

HTML:

<div id="foo" unselectable="on" class="unselectable">...</div>
Plutonian answered 17/1, 2011 at 10:48 Comment(4)
ok it will turn off the selection of text but how do i select inputs textareas and select buttons?Septuplicate
Doing this still allows you to select text within inputs and textareas.Plutonian
yeah yeah ) i've tested it, but it doesnt work in Opera... :(Septuplicate
Really? Works fine for me in Opera. Have you got a test page I can see?Plutonian
T
0

You need to prevent event bubbling for textboxes and selects.

You can use :input selector to select all input, textarea, select and button elements.

Something like

$(":input").bind("mousedown", function(e){
    e.stopPropagation();
});

See a working demo

Tion answered 17/1, 2011 at 10:35 Comment(1)
but u blocked the body content by using javascript, i wanted to block by using css, and add exceptions by javascriptSeptuplicate

© 2022 - 2024 — McMap. All rights reserved.