Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

htmldecode function in javascript

Posted on 2008-10-03
5
Medium Priority
?
8,897 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 22636660
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
ID: 22636836
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
ID: 22636916
>>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
ID: 22637010
so how do i remove special characters also
0
 
LVL 82

Accepted Solution

by:
hielo earned 1500 total points
ID: 22637045

<!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

Technology Partners: 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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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)
Suggested Courses

647 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