I am creating a graphic user interface with a drop down menu filled with css styles (bold, italics, changing color of words, etc). The problem I am running into is when I apply these styles, it applies to the entire paragraph and not the individual words. How can I code it to recognized what is highlighted and applying the formatting specifically to that.
I would like to apply span to just the highlighted portion. How?
Ex:
<style> .red { color: red; } </style> <body> <p>This <span class="red">is a</span> test.</p> </body> This is the only way I know of to apply span but would like to learn how to have it apply to what the user highlighted. Is there code that detects what user highlighted?
Edit: The goal is to highlight, apply a css style to what is highlighted, remove highlight and style is still there.
::selectionare missing the boat.::selectionallows you to set the style of the currently selected text. As soon as the selection changes those styles no longer apply. To select text and change it "permanently" you have to find what is currently selected, then manipulate the DOM via Javascript to surround that text with a<span class="something">(or "strong" or "em" as appropriate)