Solved

JavaScript: Convert HTML to XHTML

Posted on 2011-02-21
1
264 Views
Last Modified: 2012-06-21
Please help me find a JavaScript function that will convert HTML to XHTML.


alert(html2xhtml('<P><b>This <I>is a</I></b><BR><u>test</u></P>'));

Open in new window

0
Comment
Question by:hankknight
1 Comment
 
LVL 8

Accepted Solution

by:
PagodNaUtak earned 500 total points
ID: 34949029
try the below code
function convert(txt,obj){
  txt="<p>"+txt.split(/\r\n(\s+)?\r\n/).join("</p><p>")+"</p>";
  txt=txt.split("\n").join("<br>");
  txt=txt.replace(/(http:\/\/)?www\.[^\s]+/gi,function(p1){return "<a href=\""+p1+"\">"+p1+"</a>";});
  txt=txt.replace(/(\r|\n|\r\n)/gi,"");
  txt=txt.split("</p><p>").join("</p>\r\n<p>");
  obj.value=txt;
}

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question