/ Published in: jQuery

Parsing Carrage return with br tags
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>jQuery Parsers</title> <script type="text/javascript" src="../../jquery-1.3.2.min.js"></script> <script type="text/javascript"> // JavaScript Document jQuery(function($) { $('p.solutionNotes').escapeHtml(); var notesText = $('p.solutionNotes').text(); alert(notesText); $('.sNotes').html(notesText); $('#submit').click(function() { var htmlval = $('#textEntry').val(); $('p.escape').text(htmlval); $('p.escape').escapeHtml(); var parsedText = $('p.escape').text(); //alert(parsedText); $('#finalVal').val(parsedText); var final = $('#finalVal').val(); //alert(final); }); }); (function($) { ; $.fn.escapeHtml = function() { this.each(function() { $(this).html( $(this).html() .replace(/"/g,""") .replace(/&/g,'&') .replace(/</g,'<') .replace(/>/g,'>') .replace(/'/g,''') .replace(/( |[ ])/g, "<br />") .replace(/183/g,'·') //.replace(/ /g,'<br/>') ); }); return $(this); } })(jQuery); </script> </head> <body> <script type="text/javascript"> jQuery(function($) { var textTextarea = $('.escape').html(); //alert(textTextarea); //$('#textEntry').val(textTextarea); }); function parseStrings(obj){ var data = obj.form.textEntry.value; obj.form.finalVal.value = carrageReturn(data); } function carrageReturn(dataStr) { return dataStr.replace(/( |[ ])/g, "<br />") .replace(/"/g,""") .replace(/&/g,'&') .replace(/'/g,''') .replace(/[<>]/g, function (s){ return (s == "<")? "<" :">"}); } </script> <p class="escape"><big>asdfasd</big></p> <br /> <FORM id=crlf2br name=crlf2br action="test.html"> <TEXTAREA class=textarea id="textEntry" name="textEntry" rows=5 cols=75>Insert line breaks into this string by pressing enter with the cursor between the words.</TEXTAREA> <INPUT onclick='parseStrings(this)'; type="button" value="Convert to Tag"> <TEXTAREA class="textarea readonly" id="finalVal" name="finalVal "rows=5 readOnly cols=75></TEXTAREA> </FORM> <p class="solutionNotes"></p> <p class="sNotes">sdf</p> </body> </html>
Comments
