title: jQuery text() function loses line breaks in IE
body: <p>This is quite a talked about problem on the jQuery forums but I can't figure out a solution for my situation.</p>
  
  <p>I have an unordered list with some text elements in them... the user can create new list items via jQuery, which are saved to an SQL database. They can also edit existing list items. Also, because the text in each list item can get quite long, they have the option of 'hiding' each list item so it shows a truncated version of the string. This is handled by a custom jQuery plugin that truncates list items that are over a certain length... here's what each list item looks like once the truncate plugin has formatted it:</p>
  
  <pre><code>&lt;li id="item_1"&gt;
  &lt;div class="note"&gt;
      This is the text that shows when this element is truncated &lt;span style="display: none;" class="truncate_ellipsis"&gt;...&lt;/span&gt;
  &lt;span style="display: inline;" class="truncate_more"&gt;This is the text that will be hidden if the user clicks on the 'hide' button&lt;/span&gt;
  &lt;/div&gt;  
  &lt;div class="toggle_wrapper"&gt; 
      &lt;div class="note_toggle"&gt;
          &lt;a href="#" class="truncate_more_link"&gt;Hide note&lt;/a&gt;
      &lt;/div&gt; 
      &lt;div style="display: block;" class="note_controls"&gt; 
          &lt;span class="edit_note"&gt;&lt;a href="#note_textfield" id="edit_note_1"&gt;Edit&lt;/a&gt;&lt;/span&gt; | &lt;span class="delete_note"&gt;&lt;a href="#" id="delete_note_1"&gt;Delete&lt;/a&gt;&lt;/span&gt; 
      &lt;/div&gt; 
  &lt;/div&gt; 
  &lt;/li&gt;
  </code></pre>
  
  <p>The problem I have is that the user clicks on 'edit' and it takes the contents of the div with the class 'note' and assigns it to a text area. The user can then edit the text, and save it. I am using the following script to grab the contents of the div and assign it to the textarea:</p>
  
  <pre><code>$('.edit_note a').click(function(event){
  
      // Get the parent li of the 'edit' link that was clicked 
      var parentLi = $(this).closest('li');
  
      // fade out the li that's being edited
      parentLi.fadeOut('fast');
  
      // remove the '...' that shows when the note is truncated
      parentLi.find('.truncate_ellipsis').remove();
  
      // find the 'note' div within the li that's being edited
      var currentNote = parentLi.find('.note');
  
      // grab the id of this note... used when saving to the DB
      var noteId = $(this).attr('id').substr(10);
  
      // Set the current note id variable and editing status
      currentNoteId = noteId;
      currentNoteStatus = 'edit';
  
      //add the note ID as a hidden field to the text editor form
      $('input[name$="note_id"]').val(noteId);
  
      // grab the text from this note and add it to the text area
      // (textarea id is 'notes_content')
      $('#notes_content').val($.trim(currentNote.text()); // this is the text area
  
      // fade in the text area so the user can edit
      $('div.notes_textarea').fadeIn();
  
  });
  </code></pre>
  
  <p>Once it's saved I'm using a function I found online to convert the line breaks to BRs, before assigning the contents of the text area back to the 'note' div within the appropriate list item:</p>
  
  <pre><code>function nl2br (str, is_xhtml) {   
  var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '&lt;br /&gt;' : '&lt;br&gt;';    
  return (str + '').replace(/([^&gt;\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');
  }
  </code></pre>
  
  <p>This all works fine in firefox / chrome / opera / safari... however, IE gets rid of the line breaks when it grabs the text via the jQuery text() function. This is discussed in the comments of this page on the jQuery documentation:</p>
  
  <p><a href="http://api.jquery.com/text/" rel="nofollow">http://api.jquery.com/text/</a></p>
  
  <p>Some people have had success by cloning the source element, wrapping it in 'pre' tags, then taking the contents of that and putting it in the text area. This works for line breaks but also brings over extra spaces, tabs etc. and it looks a bit of a mess. A better solution seems to be to use the html() jQuery function to grab the text, then replace br's for line breaks, and strip out any other html formatting.</p>
  
  <p>I'm new to Javascript however, and I'm rubbish at regular expressions so I wouldn't have a clue how to do this and I'm running out of time! I need a regular expression, or just some help with string formatting to do the following:</p>
  
  <ul>
  <li><p>remove all span tags from a string WITHOUT removing the contents of the span (my truncate function adds a span with the class "truncate_more" ... see html code above)</p></li>
  <li><p>remove br's and replace them with newline characters that will display correctly in a textarea element and be consistent across browsers</p></li>
  </ul>
  
  <p>OR.. if anyone knows of a better workaround to this IE / textarea / linebreak issue, I'd really appreciate an idiots guide :)</p>
  
  <p>Quite a bit of information for what is probably a simple solution but I thought I'd try to explain the situation as best I could! Any help would be hugely appreciated....</p>
  
  <p><strong>EDIT</strong>: 'TheJuice's answer below worked for the br's / IE line breaks issue (thanks!), but I need to carry out a similar regex to get rid of the spans, as they only encapsulate some of the text. I tried the following, which seems to work fine in firefox but in IE the spans remain:</p>
  
  <pre><code>        var withBRs = currentNote.html();
      var textWithBreaks = withBRs.replace(/\&lt;br\&gt;/gi,'\r');
      textWithBreaks = textWithBreaks.replace(/\&lt;.*span.*\&gt;/g, '');
  </code></pre>
  
  <p><strong>ANOTHER EDIT</strong> : 'TheJuice' solved this problem too... ignore my horrific regex and just do what he says if you find yourself in the same situation! Thanks to all who offered suggestions...</p>