Get selected elememnt : Text Selection « HTML « JavaScript DHTML






Get selected elememnt

<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>

           
       








Related examples in the same category

1.Capturing a Text Selection
2.Using the TextEdit Property
3.Using the elementFromPoint() Method
4.Get selecte text in one paragraph