title: Javascript : how to insert tags around a text selection?
body: <p>I want to create a little WYSIWYG HTML text editor with a contenteditable div. </p>
  
  <p>For that, I use window.getSelection() to retrieve the selected text, and when I click in a button (button bold), I execute a function to add bold tags around the selected text. </p>
  
  <p>But I don't have any idea about the javascript code (without JQuery) to add bold tags. </p>
  
  <p>Here my code :</p>
  
  <pre><code>&lt;input type="button" value="B" style="font-weight:bold;" onclick='add_tags("b");'&gt;
  
  &lt;div id="container_contenteditable" class="container_contenteditable" contenteditable&gt;&lt;/div&gt;
  
  &lt;script type="text/javascript"&gt;
  
  function add_tags(tags)
  {
      container_contenteditable = document.getElementById("container_contenteditable");
  
      //Retrieve the selected text :
      sel = window.getSelection();
  }
  
  &lt;/script&gt;
  </code></pre>