Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Python, Lua and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, C, C++, Ruby, MySQL and Linux / Tomcat systems
JavaScript - change selected text using button

Posted by TedH (TedH), 30 October 2004
I have an editor which uses JavaScript with buttons to format HTML (bold, italic etc.). This works fine but I would like to be able to do this a different way.

In textarea in a form:
1)Select text with cursor
2)Click a button (say bold)
3)The selected text gets <b>selected text</b> HTML bold tags written around it - without moving the selected text from it's position in the rest of the text.

How do I accomplish that using JavaScript? Is it even possible?

Hope you can help.

Thanks - Ted

Posted by admin (Graham Ellis), 30 October 2004
Oh help!   I'm not personally enough of a JavaScript person to attempt to come up with even a hint of an answer (much more a server-side person myself), but acknowledging your question Ted and hoping that some of the othe folks who answer here can come up with a useful answer ....

Posted by TedH (TedH), 30 October 2004
Thought I'd stick a page up with it so folk might understand the problem more easier.

<A HREF="" target="_blank">

Any extra additions from a button autmatically puts it after the existing text.

JS is on the page (view source).

Have fun - Ted

Posted by Custard (Custard), 30 October 2004
Just had a google, and came up with this...

Which possibly looks like your man!

Couldn't test as I use Safari..

Hate to say it, but Microsoft have a quite good set of docs for DOM and JS.



Posted by TedH (TedH), 31 October 2004
I'll get that and see what I can do with it - thanks Ted

If anyone else has any ideas I'll have a look at them too - cheers

Posted by TedH (TedH), 31 October 2004
Well, it works to a degree, but not very well. I did find a couple of other bits that would turn text bold, but unfortunately it turns all the text in the form bold without any HMTL tags.

After a few hours of scrounging around the web it seems that this particular thing is in the realm of VB, C++ etc., but not JavaScript.

Well it was worth a try anyway. Looks like I shall have to simply write very exact instructions on how to use the editor. Once a user gets used to it should not be a problem.

Thanks - Ted

Posted by TedH (TedH), 2 November 2004
After a bit of thought and messing around I contacted a friend who's a JS expert and he gave me a bit of code.

While not absolutely perfect is works well enough.

The "After thought" buttons work quite well on selected text. But - can mess up things if you click a button without selecting anything. A page refresh seems to solve that okay.

- Ted

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2018: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01225 708225 • FAX: 01225 793803 • EMAIL: • WEB: • SKYPE: wellho