Parsing Carrage return with br tags


/ Published in: jQuery
Save to your folder(s)

Parsing Carrage return with br tags


Copy this code and paste it in your HTML
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html>
  3. <head>
  4. <title>jQuery Parsers</title>
  5. <script type="text/javascript" src="../../jquery-1.3.2.min.js"></script>
  6. <script type="text/javascript">
  7. // JavaScript Document
  8.  
  9. jQuery(function($) {
  10.  
  11. $('p.solutionNotes').escapeHtml();
  12. var notesText = $('p.solutionNotes').text();
  13. alert(notesText);
  14. $('.sNotes').html(notesText);
  15.  
  16. $('#submit').click(function() {
  17. var htmlval = $('#textEntry').val();
  18. $('p.escape').text(htmlval);
  19. $('p.escape').escapeHtml();
  20.  
  21. var parsedText = $('p.escape').text();
  22. //alert(parsedText);
  23.  
  24. $('#finalVal').val(parsedText);
  25. var final = $('#finalVal').val();
  26. //alert(final);
  27. });
  28.  
  29.  
  30. });
  31.  
  32. (function($) {
  33.  
  34. ;
  35. $.fn.escapeHtml = function() {
  36. this.each(function() {
  37. $(this).html(
  38. $(this).html()
  39. .replace(/"/g,"&quot;")
  40. .replace(/&/g,'&amp;')
  41. .replace(/</g,'&lt;')
  42. .replace(/>/g,'&gt;')
  43. .replace(/'/g,'&apos;')
  44. .replace(/(
  45. |[
  46. ])/g, "<br />")
  47. .replace(/183/g,'&middot;')
  48. //.replace(/
  49. /g,'&lt;br/&gt;')
  50. );
  51. });
  52. return $(this);
  53. }
  54.  
  55. })(jQuery);
  56. </script>
  57. </head>
  58. <body>
  59. <script type="text/javascript">
  60.  
  61.  
  62. jQuery(function($) {
  63. var textTextarea = $('.escape').html();
  64. //alert(textTextarea);
  65. //$('#textEntry').val(textTextarea);
  66. });
  67.  
  68. function parseStrings(obj){
  69. var data = obj.form.textEntry.value;
  70. obj.form.finalVal.value = carrageReturn(data);
  71. }
  72.  
  73. function carrageReturn(dataStr) {
  74. return dataStr.replace(/(
  75. |[
  76. ])/g, "<br />")
  77. .replace(/"/g,"&quot;")
  78. .replace(/&/g,'&amp;')
  79. .replace(/'/g,'&apos;')
  80. .replace(/[<>]/g, function (s){ return (s == "<")? "&lt;" :"&gt;"});
  81.  
  82. }
  83.  
  84. </script>
  85. <p class="escape"><big>asdfasd</big></p>
  86. <br />
  87. <FORM id=crlf2br name=crlf2br action="test.html">
  88. <TEXTAREA class=textarea id="textEntry" name="textEntry" rows=5 cols=75>Insert line breaks into this string by pressing enter with
  89. the cursor between the words.</TEXTAREA>
  90. <INPUT onclick='parseStrings(this)'; type="button" value="Convert to Tag">
  91.  
  92.  
  93. <TEXTAREA class="textarea readonly" id="finalVal" name="finalVal "rows=5 readOnly cols=75></TEXTAREA>
  94. </FORM>
  95. <p class="solutionNotes"></p>
  96. <p class="sNotes">sdf</p>
  97. </body>
  98. </html>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.