Tuesday, July 17, 2012

How to copy newline characters from HTML div to textarea in jQuery

<div id='myDiv'>
    Line 1.<br />
    Line 2<br />
    These are &ltspecial&gt; characters &amp; must be escaped !@@&gt;&lt;&gt;</div>
<input type='button' value='click' id='myButton' />
<textarea id='myTextArea'></textarea>
<script>
    $(document).ready(function () {
        $('#myButton').click(function () {
            var text = $('#myDiv').text();
            $('#myTextArea').val(text);
        });
    });
</script>
The aboce Script works in Firefox and Chrome and where asint IE7 its breaking so the code modifies like this,
$('#myButton').click(function () {
            var text = $('#myDiv').html().replace(/\<br\>/gi,"\r\n");
            $('#myTextArea').val(text);
        });

No comments:

Post a Comment