HTML contenteditable Attribute

Primero tenemos que cambiar nuestra etiqueta html a editable.

<section contenteditable="true" spellcheck="false" class="form-control caja" id="txtcuerpo" name="txtcuerpo" onfocus="true">
</section>

una ves agregado el contenteditable="true" el objeto expone el método execCommand con el que podemos manipular el contenido. por ejemplo.

document.execCommand('bold', false, null);

ahora gracias a bootstrap para darle estilo a nuestro interfaz, nuestro ejemplo quedaria haci.

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <!-- Latest compiled and minified CSS -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

        <!-- Optional theme -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

        <!-- Latest compiled and minified JavaScript -->
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
        <style>
            .caja{
                outline: none;font-size: 13px;
                background-color: #fff;
                border:1px solid #00b2ee;
                margin: 0px;
                padding: 10px;
                height: 240px;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="row">
                <div class="col-md-6 col-md-offset-3">
                    <h1>Editor de texto</h1>
                    <div class="form-inline"> 
                        <button type="button" class="btn btn-default" onclick="document.execCommand('bold', false, null);">
                            <i class="glyphicon glyphicon-bold"></i>
                        </button>
                        <button type="button" class="btn btn-default" onclick="document.execCommand('italic', false, null);">
                            <i class="glyphicon glyphicon-italic"></i>
                        </button>
                        <button type="button" class="btn btn-default" onclick="document.execCommand('underline', false, null);">
                            <i class="glyphicon glyphicon-text-width"></i>
                        </button> <!--no existia un icono para el subrayado-->
                        <button type="button" class="btn btn-default" onclick="document.execCommand('justifyFull', false, null);">
                            <i class="glyphicon glyphicon-align-justify"></i>
                        </button>
                        <button type="button" class="btn btn-default" onclick="document.execCommand('justifyLeft', false, null);">
                            <i class="glyphicon glyphicon-align-left"></i>
                        </button>
                        <button type="button" class="btn btn-default" onclick="document.execCommand('justifyCenter', false, null);">
                            <i class="glyphicon glyphicon-align-center"></i>
                        </button>
                        <button type="button" class="btn btn-default" onclick="document.execCommand('justifyRight', false, null);">
                            <i class="glyphicon glyphicon-align-right"></i>
                        </button>
                        <select title="Etiquetas" class="form-control data-select" id="etiquetas" name="etiquetas" onchange="
                                var divEtiqueta = document.getElementById('etiquetas').value;
                                document.execCommand('insertHTML', false, divEtiqueta);
                                ">
                            <option>Hola 1</option>
                            <option>Hola 2</option>
                            <option>Hola 3</option>
                            <option>Hola 4</option>
                        </select>
                        <button type="button" onclick="guardar();" class="btn btn-success">
                            <i class="glyphicon glyphicon-floppy-saved"></i>
                        </button>
                    </div>
                    <br><br>
                    <div>
                        <section contenteditable="true" spellcheck="false" class="form-control caja" id="txtcuerpo" name="txtcuerpo" onfocus="true">

                        </section>
                    </div>
                </div>
            </div>
            <script>
                function guardar(){
                    var cuerpo = document.getElementById('txtcuerpo').innerHTML;
                    alert(cuerpo);
                }
            </script>
        </div>
    </body>
</html>

 

Resultado.

Editor de texto
Aqui más comandos que podemos utilizar.