Get selecte text in one paragraph : Text Selection : HTML : JavaScript DHTML examples (example source code) Organized by topic

JavaScript DHTML
C++
PHP
JavaScript DHTML Home »  HTML   » [  Text Selection  ]   
 



Get selecte text in one paragraph

<!-- 
     Example File From "JavaScript and DHTML Cookbook"
     Published by O'Reilly & Associates
     Copyright 2003 Danny Goodman
-->

 <p onmouseup="selection2Element()" onselectstart="saveStart()">Lorem ipsum dolor sit amet, consectetaur 
adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. </p>


// 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.");
    }
}


           
       
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








Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.