devxlogo

Replace Selected Content

Replace Selected Content

Question:

How do I replicate the pasteHTML function supported by Internet Explorer in Netscape? Rewriting the layer isn’t the problem. The problem is determining the position of the text returned by the getSelection(). For example, how would I allow a user to select a piece of text on the screen and modify it (i.e. make it bold)? If the selected word appears more than once in the text, I can’t just find() it.

Answer:

You will not be able to do what you want with Navigator. IE supports replacement of the selected content. Navigator 4.x does not. All you can do to replace the contents with Navigator (at least with Navigator 4.x at this time) is to use document.write to a positioned DIV or SPAN block, or to a layer. You won’t be able to replace content based on a person selecting text from the page.

The best you can hope for is layer your entire Web page, capture the mouse click events for each layer, and when the person “applies” something to the selection, recreate the entire block, but this time add the strong tags (““) to the “selected” content. This means you would need to be able to recreate each layer, so you’re probably looking at putting page content into an array for just this purpose. This could be messy, but it is a place for you to start if you absolutely have to have this type of functionality.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist