1. // code for IE
2. var textarea = document.getElementById("textarea");
3.
4. if (document.selection)
5. {
6. textarea.focus();
7. var sel = document.selection.createRange();
8. // alert the selected text in textarea
9. alert(sel.text);
10.
11. // Finally replace the value of the selected text with this new replacement one
12. sel.text = '<b>' + sel.text + '</b>';
13. }
14.
15.
16.
17. // code for Mozilla
18.
19. var textarea = document.getElementById("textarea");
20.
21. var len = textarea.value.length;
22. var start = textarea.selectionStart;
23. var end = textarea.selectionEnd;
24. var sel = textarea.value.substring(start, end);
25.
26. // This is the selected text and alert it
27. alert(sel);
28.
29. var replace = '<b>' + sel + '<b>';
30.
31. // Here we are replacing the selected text with this one
32. textarea.value = textarea.value.substring(0,start) + replace + textarea.value.substring(end,len);
No comments:
Post a Comment
Thank you for your valuable comments.