Javascript Selection Issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vanditnagar
    New Member
    • Sep 2008
    • 1

    #1

    Javascript Selection Issue

    Hi,

    I am facing the difficulty in the selecting a text on the browser . And highlighting the text.
    I am using the window.getSelec tion() to get the object and after that i am using the range.getRangeA t(0).cloneConte nts() method for getting the copy of the document content my requirement is to get the exact html text . But when we are assigning the text into the innerHTML it complete the DOm structure i m not getting the exact HTML text of the document . For example :- if user select the text <i>"why"</i></b> now i m getting the html text as <b><i>"why"</i></b> after applying the below code code. This is the code that i m using :-

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>The serialized HTML of a selection in Mozilla and IE</title>
    <script type="text/javascript">
    function getHTMLOfSelect ion () {
    var range;
    if (document.selec tion && document.select ion.createRange ) {
    range = document.select ion.createRange ();
    return range.htmlText;
    }
    else if (window.getSele ction) {
    var selection = window.getSelec tion();
    if (selection.rang eCount > 0) {
    range = selection.getRa ngeAt(0);
    var clonedSelection = range.cloneCont ents();
    var div = document.create Element('div');
    div.appendChild (clonedSelectio n);
    return div.innerHTML;
    }
    else {
    return '';
    }
    }
    else {
    return '';
    }
    }
    </script>
    </head>
    <body>
    <div>
    <p>Some text to test the selection on.
    Kibology for <b>all</b>. All <i>for</i> Kibology.
    </p>
    </div>
    <form action="">
    <p>
    <input type="button" value="show HTML of selection"
    onclick="this.f orm.output.valu e = getHTMLOfSelect ion();">
    </p>
    <p>
    <textarea name="output" rows="5" cols="80"></textarea>
    </p>
    </form>
    </body>
    </html>

    Assist me to get rid from this problem.....

    Vandit
  • pronerd
    Recognized Expert Contributor
    • Nov 2006
    • 392

    #2
    Try setting it to the .innerText attribute vs the .innerHTML attribute.

    Comment

    • pronerd
      Recognized Expert Contributor
      • Nov 2006
      • 392

      #3
      Ahhh.... I think I might be seeing the problem. You are using the DOM API's with what is probably a String object being returned from getRangeAt(). The DOM API's are esceaping the tags included in the String object.

      So instead of this
      Code:
      var clonedSelection = range.cloneContents(); 
      var div = document.createElement('div'); 
      div.appendChild(clonedSelection); 
      return div.innerHTML;
      Try this
      Code:
      var clonedSelection = range.cloneContents(); 
      return clonedSelection.toString();

      P.S. Please use code tags when pasting code.

      Comment

      Working...