title: How to insert hyperlink into text using jQuery?
body: <p>I'd like to make an <code>Insert Link</code> button. Here is what I came up with:</p>
  
  <pre><code>$(document).ready(function(){
  function HyperLink(elementID, openTag, closeTag) {
      var textArea = $('#' + elementID);
      var len = textArea.val().length;
      var start = textArea[0].selectionStart;
      var end = textArea[0].selectionEnd;
      var selectedText = textArea.val().substring(start, end);
      var replacement = openTag + selectedText + closeTag;
      textArea.val(textArea.val().substring(0, start) + replacement + textArea.val().substring(end, len));
  }
  
  $('#btnHyperlink').click(function() {
      HyperLink("id_description", "&lt;a&gt;", "&lt;/a&gt;");
  });
  });
  </code></pre>
  
  <p>The snippent above embraces text with <code>&lt;a&gt;...&lt;/a&gt;</code>, however I don't know how to insert href to the text. </p>
  
  <p>Appreciate your help.</p>
  
  <p><strong>Note:</strong></p>
  
  <p>I want links to be dynamically added to the text, as this button is going to be used in a simple text editor. </p>