Solved

htmldecode function in javascript

Posted on 2008-10-03
5
8,877 Views
Last Modified: 2008-10-13
I need an HTMLDecode function in javascript..

I read a data in Javascript as document.getElementById("test").innerHTML;

If i need to remove any html data while reading how do I do that..for e.g. the data may contain   etc

how do parse any data or decode the data. in javascript
0
Comment
Question by:intikhabk
  • 3
  • 2
5 Comments
 
LVL 82

Expert Comment

by:hielo
Comment Utility
try:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
<title></title>
<style type="text/css">
<!--
.x{color:blue;}
-->
</style>
<script type="text/javascript">
function decodeHTML(e){
	var clean = document.getElementById(e).innerHTML.replace(/<[^>]*>/g,"");
	alert( clean );
return clean;
}
</script>
</head>
<body>
<div id="elem1" onclick="decodeHTML(this.id)">
	<span class="x">Hello</span> <strong>world</strong>
</div>
</body>
</html>

Open in new window

0
 

Author Comment

by:intikhabk
Comment Utility
but if the text is Hello World&nbsp;
it does not work properly...
for eg: if it is
<div id="elem1" onclick="decodeHTML(this.id)">
      Hello World&nbsp;
</div>

the alert returns Hello World&nbsp;
0
 
LVL 82

Expert Comment

by:hielo
Comment Utility
>>but if the text is Hello World&nbsp
Better yet, what if it is:
Procter & Gamble

have you tried a single ampersand?
0
 

Author Comment

by:intikhabk
Comment Utility
so how do i remove special characters also
0
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
Comment Utility

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

 "http://www.w3.org/TR/html4/loose.dtd"> 

<html>

<head>

<title></title>

<style type="text/css">

<!--

.x{color:blue;}

-->

</style>

<script type="text/javascript">

//you will need a "replacement table" so that the script below can make the proper

//replacements you wish

var entities=[

			['&amp;','&'],

			['&nbsp;',' ']

		];

function decodeHTML(e){

	var clean = document.getElementById(e).innerHTML.replace(/<[^>]*>/g,"");

	for( var i=0, limit=entities.length; i < limit; ++i)

	{

		clean = clean.replace( new RegExp(entities[i][0],"ig"), entities[i][1]);

	}

	alert( clean );

return clean;

}

</script>

</head>

<body>

<div id="elem1" onclick="decodeHTML(this.id)">

	<span class="x">Hello</span> &nbsp; <strong> & world</strong>

</div>

</body>

</html>

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to count occurrences of each item in an array.

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now