Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

inputting around a selection

Status
Not open for further replies.

math

Programmer
Mar 21, 2001
56
0
0
BE
Hi,

I have a textarea and I'd like to input some tags around a selection that the user makes... Like adding <B> and </B> around it, so it can later be used on a webpage...

I can get it at the end of the textarea but I'd like it to be around the selected text :(

Here's what I've got so far:
Code:
<HTML>
<HEAD>
<script language=&quot;JavaScript&quot;>
<!-- Hide

function end(){
        document.editor.area.value = document.editor.area.value + &quot;\n</body>\n</html>\n&quot;;  
}

function preview(){
        temp = document.editor.area.value;
        preWindow= open(&quot;&quot;, &quot;preWindow&quot;,&quot;status=no,toolbar=no,menubar=yes&quot;);
        preWindow.document.open();
        preWindow.document.write(temp);
        preWindow.document.close();
}

function bold() {
		text = (document.all) ? document.selection.createRange().text : document.getSelection();
		document.editor.area.value = document.editor.area.value + &quot;<b>&quot;+text+&quot;</b>&quot;;
}
// --> De-Hide
</script>
</HEAD>
<BODY>
<form name=&quot;editor&quot;>
<center>
<textarea name=&quot;area&quot; rows=12 cols=50 wrap=physical>
</textarea>
<p> 
<table border=0>
<tr>
<td valign=top>
<input type=&quot;button&quot; value=&quot; Bold &quot; onClick=&quot;bold()&quot;>   
<input type=&quot;button&quot; value=&quot; Italics &quot; onClick=&quot;italic()&quot;>   
<input type=&quot;button&quot; value=&quot; Underline &quot; onClick=&quot;underline()&quot;><br>
<input type=&quot;button&quot; value=&quot; Line Break &quot; onClick=&quot;lbreak()&quot;> 
<input type=&quot;button&quot; value=&quot; Paragraph Break&quot; onClick=&quot;pbreak()&quot;>
</td>
</tr>
</table>
</center>
<p>
</form>
</BODY>
</HTML>

I have no clue how I have to get it around the selected area, now the selected area gets copied at the end of the textarea :(

Thanx in advance,
math
 
thanks for the code math. I'm going to bed less stupid tonight. I didn't know about the getSelection() method or rather I just read it too fast to put it in memory. :)

Here is what you wanted.

function bold()
{
text = (document.all) ? document.selection.createRange().text : document.getSelection();
var allText = document.editor.area.value;
document.editor.area.value = document.editor.area.value.replace(text, &quot;<b>&quot; + text + &quot;</b>&quot;)
} Gary Haran
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top