title: jQuery - text insert into textarea only works sometimes
body: <p>I'm unclear as to why this seems to only sporadically work.</p>
  
  <p>I'm trying to create a button that allows the user to insert text into a textarea, at the location of the cursor. It works so long as they haven't typed in the textarea, but once they type the button ceases to function.</p>
  
  <p>The JS object in the script is using these two methods to split the text, then enter the text it grabs from the clicked button's 'hidden' span.</p>
  
  <p>It should change from this:</p>
  
  <pre><code>&lt;newform&gt;
      &lt;box&gt;
      &lt;/box&gt;
  &lt;/newform&gt;
  </code></pre>
  
  <p>…to include extra box tags wherever the cursor might be.</p>
  
  <pre><code>&lt;newform&gt;
      &lt;box&gt;
      &lt;/box&gt;
      &lt;box&gt; //as if the cursor was here
      &lt;/box&gt;
  &lt;/newform&gt;
  </code></pre>
  
  <p><strong>The other buttons are currently non-functional.</strong> This is a copypaste from a bigger project.</p>
  
  <pre><code>//returns an array where array[0] is text before the cursor
  //and array[1] is the text after the cursor
  split_at_cursor: function (element) {
      var $el = $(element).get(0);
      var split_pt = $el.selectionStart;
      return [$el.value.substring(0, split_pt), $el.value.substring(split_pt)];
  },
  
  //uses split_at_cursor to insert data at the cursor's location
  insert_at_cursor: function (element, to_insert) {
      if (typeof (to_insert) !== "string") {
          //this is going to change
          to_insert = '*ERROR*';
      }
      var $el = $(element);
      $el.focus();
      var split_string = fmlpad.split_at_cursor($el);
      $el.text(split_string[0] + to_insert + split_string[1]);
  },
  </code></pre>
  
  <p>See the full working non-working example <a href="http://jsfiddle.net/DUjxh/1/" rel="nofollow">here</a></p>
  
  <p>I've tried some console logging to see what the nature of the issue might be, but it would appear that it always correctly determines where the text <em>should</em> go, and what the inserted text <em>should</em> be -- it just doesn't always actually do the inserting! :/</p>
  
  <p>Also, I realize I'm probably using a few too many calls to <code>$(some_element)</code>, but I'm not sure that the problem lies there, and those will come out once the app gets a little tighter and better defined.</p>