Rich Text Editor in javascript
- by chanthou
iframe
        
            .text-bold{
                border:1px solid orange;
                background-color:#ccc;
                width:16px;
                height:16px;
                font-weight:bold;
                cursor:pointer;
            }
            .active{
                border-color:#9DAECD #E8F1FF #E8F1FF #9DAECD;
                background-color:yellow;
            }
        
        
            function init( ) {
                iframe = document.createElement("iframe");
                document.body.appendChild(iframe);
                iframe.onload = setIframeEditable;
            isBold=false;
            div=document.getElementById("bold");
        }
        var setIframeEditable = function(){
            iframe.contentDocument.designMode='on';
            iframe.focus();
        }
        function makeBold(){
            if(!isBold){
                //console.log(iframe.contentDocument.execCommand("bold", false, null));
                iframe.contentDocument.execCommand("bold", false, null);
                div.className += " active";
                isBold=true;
                iframe.focus();
            }else{
                //console.log(iframe.contentDocument.execCommand("bold", true, null));
                iframe.contentDocument.execCommand("bold", false, null);
                div.className ="text-bold";
                isBold=false
                iframe.focus();
            }
        }
    </script>
</head>
<body onload="init()">
    <div id="bold" class="text-bold" onclick="makeBold()">B</div>
</body>