javascript - set selected word in bold in html document -


what want user can select text in html document , selected text if not set bold. if bold set regular. found example on stackoverflow here , wanted try out.

this test code:

<html> <head>    <title>test</title> </head> <body> <script type="text/javascript" language="javascript">    function togglebold() {     var range, sel;     if (window.getselection) {         // non-ie case         sel = window.getselection();         if (sel.getrangeat) {             range = sel.getrangeat(0);         }         document.designmode = "on";         if (range) {             sel.removeallranges();             sel.addrange(range);         }         document.execcommand("bold", false, null);         document.designmode = "off";     } else if (document.selection && document.selection.createrange &&             document.selection.type != "none") {         // ie case         range = document.selection.createrange();         range.execcommand("bold", false, null);     } } </script>  <div onselect="togglebold()">this test.</div>  </body> </html> 

when i'm testing in chrome, safari or whatever nothing happens.

i'm not javascripter don't know how debug this.

an alternative answer keep away inline javascript. togglebold() function work, although it's not actioning upon event, select event works on elements actioned <form> elements.

<span>here text</span>  <script>    /**     *   listen 'mouseup' event, actioned     *   after mouse click released after selection.    **/    window.addeventlistener( 'mouseup', togglebold, false );     function togglebold() {       //toggle's text.    } </script> 

demo fiddle: http://jsfiddle.net/qrypz/


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -