Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <script>
  4. function Highlight()
  5. {
  6. //alert(text.focusOffset - text.anchorOffset);
  7.  
  8. var text = window.getSelection();
  9. var start = text.anchorOffset;
  10. var color = document.getElementById("color").value;
  11.  
  12. var end = text.focusOffset - text.anchorOffset;
  13.  
  14. range = window.getSelection().getRangeAt(0);
  15. range1 = window.getSelection().toString();
  16. var selectionContents = range.extractContents();
  17. var span = document.createElement("span");
  18.  
  19. span.appendChild(selectionContents);
  20.  
  21. span.setAttribute("class", "uiWebviewHighlight");
  22. span.style.backgroundColor = color;
  23. span.style.color = "black";
  24.  
  25. range.insertNode(span);
  26. }
  27.  
  28. function bold(){
  29. var text = window.getSelection();
  30. var start = text.anchorOffset;
  31. var end = text.focusOffset - text.anchorOffset;
  32. range = window.getSelection().getRangeAt(0);
  33. range1 = window.getSelection().toString();
  34. var selectionContents = range.extractContents();
  35.  
  36. var bTag = document.createElement("b");
  37.  
  38. bTag.appendChild(selectionContents);
  39.  
  40. range.insertNode(bTag);
  41. }
  42.  
  43. function underLine(){
  44. var text = window.getSelection();
  45. var start = text.anchorOffset;
  46. var end = text.focusOffset - text.anchorOffset;
  47. range = window.getSelection().getRangeAt(0);
  48. range1 = window.getSelection().toString();
  49. var selectionContents = range.extractContents();
  50.  
  51. var uL= document.createElement("u");
  52.  
  53. uL.appendChild(selectionContents);
  54.  
  55. range.insertNode(uL);
  56. }
  57.  
  58. function display(){
  59. var cont= document.getElementById("div1").innerHTML;
  60. document.getElementById("div2").innerHTML=cont;
  61. alert(cont);
  62. }
  63.  
  64. </script>
  65. <body>
  66.  
  67. Select your favorite color: <input id="color" type="color" name="favcolor" /><br />
  68. <div contenteditable="true" id="div1">Edit the code above and click to see</div>
  69. <div id="div2"></div>
  70. <button id="btn" onClick="Highlight()">Get Color</button>
  71. <button id="btn1" onClick="bold()">BOLD</button>
  72. <button id="btn2" onClick="underLine()">UnderLine</button>
  73. <button id="btn3" onClick="display()">Alert the div content</button>
  74.  
  75. </body>
  76. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement