title: How to replace a specific line in textarea?
body: <p>I'm trying to replace just one line in a textarea</p>
  
  <p>I'm trying to change that line so it is updated when a selection is made elsewhere on the same form.</p>
  
  <p>I thought it may be best to read the textarea into an array, amend the array element as needed and then write this back to the text area.</p>
  
  <p>This works, but I'm appending it to the current values, not replacing them.</p>
  
  <p>How do I replace them ?</p>
  
  <p>or is there a better way to do this ?</p>
  
  <p>This is the text area:</p>
  
  <pre><code>&lt;textarea  name="msg" id="msg"  style="height: 150px; width: 300px;"&gt;
  first line is OK
  sendline is also fine
  
  new val=
  this line is fine    
  &lt;/textarea&gt;
  </code></pre>
  
  <p>The line I want to replace is <code>new val=</code>
  and I'm trying to replace that with <code>new val=</code> and the value from <code>$('#test').val();</code></p>
  
  <p>This is the jquery I have so far:</p>
  
  <pre><code>$('#test').on('change', function() {
      var test = $(this).val();      
      var lines = $('#msg').val().split(/\n/);
      lines[2] = "new val= " + test;
      $.each(lines , function(i, val) { 
          $("#msg").append("\r\n" + lines[i]);
      }) 
  </code></pre>
  
  <p>and a <strong><a href="http://jsfiddle.net/84pk258r/" rel="nofollow">fiddle</a></strong> showing what it's doing.</p>
  
  <p>any ideas ?</p>
  
  <p>Thanks</p>