Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do you populate a textarea with jquery and linebreaks?

Tags:

jquery

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);


});
like image 648
willdanceforfun Avatar asked Aug 07 '09 00:08

willdanceforfun


2 Answers

Try doing:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <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

like image 141
Jim Schubert Avatar answered Sep 28 '22 18:09

Jim Schubert


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);


});
like image 30
Sorantis Avatar answered Sep 28 '22 18:09

Sorantis