Click to See Complete Forum and Search --> : HELP! - onselectstart


stefanb
10-26-2003, 06:00 PM
Hi!
I have a webpage where I have "inactivated" selecting text from the page using:

document.onselectstart = function() {return false}

This works... but it works too good in my case...
I have a couple of input fields (textboxes and textareas) on the page and I want the user to be able to select text within those areas but nowhere else on the page.

Anybody have any ideas of how to fix this?

<edit>
The script will only have to work in IE5+
</edit>

fredmv
10-26-2003, 06:46 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>untitled</title>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=iso-8859-1" />
<script type="text/javascript">
/*<![CDATA[*/
var e = null;

document.onselectstart = function()
{
e = event.srcElement;
return ( e.type == "text" || e.type == "textarea" );
}
/*]]>*/
</script>
</head>
<body>
<div>
<span>You can't select me.</span>
<form id="form" style="margin: 0px;" action="#">
<input type="text" value="But you can select me." />
<br />
<textarea rows="10" cols="50">You can select me as well.</textarea>
</form>
</div>
</body>
</html>

stefanb
10-26-2003, 09:13 PM
It works great!!!

Thanks for all help!!!

fredmv
10-26-2003, 09:40 PM
You're very welcome. I'm happy that helped you out. :D