Click to See Complete Forum and Search --> : apply code to selected text in textarea


lucienne
06-20-2003, 02:24 AM
I'm trying to figure out how to apply a piece of code around a selected text in a textarea. For instance:
a textarea has the following text:
"Hi my name is"
I select the text "name"
When I press a button I want the tags <b></b> around the selected text "name".
I have a script to put the tags in the textarea I just don't know how to get the tags around the selected word.
Does anybody have an idea?

Gollum
06-20-2003, 03:32 AM
Have a play around with this...


<html>
<head>
<script language="JavaScript">
function Click()
{
if ( document.selection )
{
var oRange = document.selection.createRange();
oRange.text = '<b>' + oRange.text + '</b>';
}
}
</script>
</head>

<body>
<button onclick="Click();">Click</button><br>
<textarea cols=80 rows=10 id=tb>Here is some text</textarea>
</body>
</html>


down side is it only works on IE. Anyone with ideas for Netscape and otheres?

lucienne
06-20-2003, 03:38 AM
Thank you Thank you Thank you!!
You're the greatest!

Gollum
06-20-2003, 04:12 AM
<blush>You're too kind :) </blush>