<html> <head> <script> function selection2Element() { var rng = document.selection.createRange(); var newHTML = "<span class='newSpan'>" + rng.text + "</span>"; rng.pasteHTML(newHTML); } </script> </head> <body> <p onmouseup="selection2Element()">select me</p> </body> </html>