tags:

views:

624

answers:

3

Hi there, I know how to populate a textarea, but how to populate it so that it maintains the line breaks?

e.g

html

    <div id="previous_purchases">blah blah blah<br />blah blah</div>

jquery

    $('#previous_purchases').click(function(){

  var what = $(this).text();

  $('#purchased').text(what);


});

All the blah's just rock up in the textarea on one line. Any ideas?

edit: I have been trying with html() instead of text but it produces the same result. I would imagine by using the html() I would end up with a textarea that had '
' but that is not the case... its just all on one line.

Even with this code:

    $('#previous_purchases').click(function(){

  var what = $(this).html();

  $('#purchased').html(what);


});
+3  A: 

Try doing:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title></title>
        <script src="scripts/jquery-1.3.2.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(document).ready(
              function() {
                 $('#previous_purchases').click(function() {
                 var what = $(this).html().replace(/<br>/, '\n');
                 $('#purchased').html(what);
            });
        });
        </script>
    </head>
    <body>
    <div id="previous_purchases">blah blah blah<br />blah blah</div>
    <textarea id="purchased" cols="25" rows="10"></textarea>
    </body>
    </html>

See Attributes/html

Jim Schubert
sorry I should have clarified, html() was my first attempt at this.
cosmicbdog
I should have been a little more clear myself. You'll need to pull html into "what" and add it to #purchased using html() for both. I edited my comment with an entire HTML example which works.
Jim Schubert
sorry if that first sentence about sounds abrupt, it's not meant to. I hope this works for you!
Jim Schubert
thats alright. i didn't take it that way. normally the above code works for me, but i'm filling a textarea, not just a div. If I do the above, it updates a div nicely and maintains the line breaks. but a textarea, it doesn't.
cosmicbdog
I fixed the code to work with textarea. I wasn't aware that IE supports breaks in textarea while Firefox and Chrome don't (I used DIV assuming the output would be the same). So, you'll have to regex replace the line breaks with new lines as mentioned a couple posts below
Jim Schubert
thats it! thanks heaps jim
cosmicbdog
its not working for me and I am searching for this: #("textarea").html("blah\nblah") does not work in IE7!
CallMeLaNN
+1  A: 

Instead of calling text() function you can use html() and path formatted text to it.

$('#previous_purchases').click(function(){

                var what = $(this).text();

                $('#purchased').html(what);


});
Sorantis
This doesn't appear to work for me :| i mean it should... my head says it should...
cosmicbdog
+2  A: 

text() strips out html automatically. So basically you'll want to use html() instead of text, and replace
with \n

Mark
would you be able to explain more about inserting the \n? thats how textarea interprets the new lines... but how to put them in?
cosmicbdog