go***********@gmail.com wrote:
I have a textfield and you of course you can select text...
When they hit the bold button I want it to obtain the selected text and
bold it, the hard part is trying to figure out if javascript can even
OBTAIN selected text?!?!
As it was mentioned above, text field, textarea and other form elements
are "atomic formatting" fragments. It means that you can apply nearly
any formatting to them but to the *entire element only*. So say having
<input type="text" name="txt0" value="Lorem ipsum"> you can make "Lorem
ipsum" text bold, italic, green etc. but you cannot make say "Lorem"
bold and "ipsum" normal. This is how it was made and no programming
language (JavaScript or Java or any other) will change it.
But let's us assume that you just did not express yourselve properly
and you are working with a custom made pseudo-textarea based on <div>
or similar.
In this case you hit the issue that in order to properly apply the
formatting you need *two* pieces of info:
1) Text content fragment to apply formatting to
2) Exact position of this content fragment in the document.
The first data are equally provided by all existing browsers: TextRange
in IE, getSelection() in NN, FF, Safari and others.
Unfortunately the second piece of info provided by IE only via
TextRange properties. Other rivals still suck. It means that in the
context: "Some text text text" you can select first "text" and
setSelection(), but you have no means to know which one of these three
"text" should be displayed bold.
If you don't care about others besides IE, just go ahead with
TextRange.
<http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/objects/obj_textrange.asp>