467,122 Members | 1,329 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,122 developers. It's quick & easy.

Writing HTML tags around highlighted form text in Safari

Hi

I'm trying to write a system in which you can highlight some text in a form
box, click a button (or press a key combination) and the text highlighted
gets surrounded by a given html tag.

The following works fine in IE on a PC:
txt = document.selection.createRange().text;
document.selection.createRange().text = '<b>' + txt + '</b>';
I gather that Safari "doesn't do" document.selection.createRange().text, so
I'm using window.getSelection() instead.

I can use the following to demonstrate for myself that safari is "seeing"
the highlighted text in the form box:
txt = window.getSelection();
alert(txt);
window.getSelection() = '<b>' + txt + '</b>';
The last line is doing nothing though. I've also tried:
window.getSelection().text = '<b>' + txt + '</b>';
but no luck.

Any ideas how I can write HTML tags around some highlighted text in Safari?

Thanks for any help,

Julian
Jul 23 '05 #1
  • viewed: 1590
Share:
2 Replies
VK
> In Safary
txt = window.getSelection();
alert(txt);


Would be greate to hear from experienced Safary victime (I'm victimized
by Windows :) But from the first look Safary simply implemented the
ancient NN4.x method. In this case getSelection()
1) is read only
2) it's just selected text on the page, with no correlation to the
actual node position.

So if you have a text ( where [ ] mark the selected text):

<p>Something just to show the problem and why this [problem] cannot be
solved.</p>

and you try to use DOM methods to locate the right node based on the
selectedText() value, you will get the first "problem" (because it
stays first), not the selected one.

If Safari *really* doesn't have any more serious methods than this,
then just screw on it until it will grow up a bit.

Jul 23 '05 #2
VK wrote:
In Safary
txt = window.getSelection();
alert(txt);


Would be greate to hear from experienced Safary victime


good hunting ;-)

Robi "one pun in ten did" :-)
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Christophe Guillon | last post: by
385 posts views Thread by Xah Lee | last post: by
17 posts views Thread by Eric Lindsay | last post: by
10 posts views Thread by Eric Lindsay | last post: by
2 posts views Thread by justplain.kzn@gmail.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.