Jump to content

How do I get selected text?


migroo

Recommended Posts

Okay I want to make is so if some one selects some text inside of a textfield and hits a button my javascript function can get that text.Something like:

var stxt = "the text the user has selected.";document.getElementById('txt').innerHTML = "<p style='color:pink;'>"+ stxt +"</p>";

I want to add more but I want so be able to put the selected text into a variable so I can add style's to it.Also is there a way to put the prompter's position into a variable?Thanks a lot guys!

Link to comment
Share on other sites

You might think there would be an easy solution to this, but there is not. A cross-browser solution to getting the selection is possible, but very complicated. I can show you some code if you want. You won't like it.But there is a fundamental problem with your plan. When the user clicks a button, the text input loses its focus, and the selection disappears. This is true in Opera and IE.In FF and Safari, the values used to determine the selection actually persist, but there is no guarantee that this will be true in future versions. This persistence can also cause other potential problems, depending on how the script is written.What I'm really saying is, if this thing can only be triggered by clicking on something, I don't think it can work.

Link to comment
Share on other sites

Okay. That is too bad. What about just getting where the prompter is?So I could add in like a smiley when they click on one.Thanks for your help. Why don't they make it easier to edit text on the web?

Link to comment
Share on other sites

Getting the cursor in a cross-browser way is basically the same problem as before.HTML 5 adds support for editing text right on the page. The contenteditable property is available in all major browsers NOW. It's worth looking at. But support is inconsistent, and some of the behavior can be strange.If you want rich text editing, have you checked out tinyMCE?

Link to comment
Share on other sites

WOW! No I haven't checked tinyMCE out but I am now. It looks great!

Link to comment
Share on other sites

Well I checked into it and messed around with it today. I love it! It does everything I want to do and I don't have to re-invent it. THANK YOU VERY MUCH!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...