Get selecte text in one paragraph : Text Selection « HTML « JavaScript DHTML






Get selecte text in one paragraph

<html>
<head>
<script>

// global
var selectionStart;

// invoked by onselectstart
function saveStart() {
    selectionStart = event.srcElement;
}
// invoked by onmouseup
function selection2Element() {
    if (event.srcElement == selectionStart) {
        var rng = document.selection.createRange();
        var newHTML = "<span class='newSpan'>" + rng.text + "</span>";
        rng.pasteHTML(newHTML);
    } else {
        alert("Please restrict selections to within a single paragraph.");
    }
}
</script>
</head>
<body>
 <p onmouseup="selection2Element()" onselectstart="saveStart()">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 selected elememnt