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!

javascript + creating tag editor

Status
Not open for further replies.

LordGarfield

IS-IT--Management
Jul 31, 2003
112
BE
Hello,

What I was wondering if it is possible to get something like this.

A user fills in a textarea. With a certain layout. But he wants to put text in bold to. Is there a way that the user can select a part out of the textarea. presses a button in the form wich says B from bold. And then Javascript will add before the selected text and after selected text.

It would be nice. I can do it so that if you press a button once it adds on the curront position and if you press it again it adds but it would be nicer if you can select text press the button and the tags are added.

Tanks already.

PS if you can do it in another way so let's say java. then I can try that one to but I prefere Java Script.
 
Hi LordGarfield!

Have a look here:


I think this is exactly what you're looking for §;O)

dkdude1.gif


Jakob
 
That is indeed a verry good trick to do it. and it is idd verry useable. but still I want to know how I can determine a selection in a textbox. (you never know enough).

Is there a way that I have a var wich is like textbox.selstart and textbox.sellen our something?

Tanks already.
 
Anyway I found it and created it.

I post my source code.

put this into notepad and call it roelseditor.js :)

function tag(t)
{
var detext = document.selection.createRange().text;
if (detext)
{
document.selection.createRange().text = "[" + t + "]" + detext + "[/" + t + "]";
}
}

function smile(s)
{
var post = window.document.editform.post.value;
window.document.editform.post.value = post + s;
window.document.editform.post.focus();
}

Then in your HTML have something like this.
<html>
<head>
<title>Roels Editor</title>
<script src=&quot;roelseditor.js&quot;></script>
</head>
<body>
<form action=&quot;someurl wich processes the form&quot; method=&quot;post/get&quot; name=&quot;editform&quot;>
<p>
<input type=&quot;button&quot; value=&quot;B&quot; name=&quot;b&quot; onclick=&quot;javascript:tag('b');&quot; />
<input type=&quot;button&quot; value=&quot;U&quot; name=&quot;u&quot; onclick=&quot;javascript:tag('u');&quot; />
<input type=&quot;button&quot; value=&quot;I&quot; name=&quot;i&quot; onclick=&quot;javascript:tag('i');&quot; />
<input type=&quot;button&quot; value=&quot;:)&quot; name=&quot;s1&quot; onclick=&quot;javascript:smile('[:)]');&quot; />
</p>
<p>
Post:<br>
<textarea rows=&quot;10&quot; cols=&quot;100&quot; name=&quot;post&quot;></textarea></p>
<input type=&quot;submit&quot; name=&quot;go&quot; value=&quot;GO&quot; />
</form>
</body>
</html>
 
Oh I forgot to say : this only works for IE 4.0 and higher.

Not for Mozilla and netscape. I need to do it elseway.


tanks for your responses.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top