Forum Moderators: open

Message Too Old, No Replies

how to disallow selecting text in a div

maybe with javascript or css

         

johnl

9:14 am on Oct 25, 2005 (gmt 0)

10+ Year Member



Hello webmasterworld,

given the following html:


<html>
<body>
<div>some text</div>
</body>
</html>

you normally can select (parts of or all of) 'some text'. How would you disallow such a selection?

Thank you and have a nice day!

BlobFisk

9:51 am on Oct 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could layer another <div> over the text div with a transparent image filling it. That way when the user tries to select the text they are unable as the top most layer is the image.

Why would you want to do this though?

johnl

10:57 am on Oct 25, 2005 (gmt 0)

10+ Year Member



Why would you want to do this though?

(ah, yes - the eerie secrets of frenzied programmers:-)

This <div> I mentioned is to be some kind of a top bar of a draggable window and contains some title. If the user grabs it with the mouse for moving the window, more often than not this title is selected - which I find annoying.

Secondly, the mechanics of the standard event handling (as in marking text) of the browsers are also of interest to me.

Thank you for your answer, I will investigate, though I would prefer a solution which tackles the involved events.

Robin_reala

11:46 am on Oct 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There is a select event and associated onselect handler, but as far as I can tell it's not part of the specs, and it seems to only be supported in text inputs and textareas.