Geeks With Blogs

News

Agha Usman Ahmed JILAWATAN
Some days back, one of my team members came to me saying he need a function which is capable of remove the html tags using javascript. I mean if we pass “<span style=’mystyle’> this is demo </span>” this string to the function. It supposes to return only “This is demo” and the main thing is with having no formatting at all, Just the simple text between tags.
So, Initially I make it happen using regular expression but the strategy fails when some think like &nbsp; comes in. after snatching my hairs, putting some hard hand on the keyboard just before the moment I go for suicide J the following code did the trick.
 
       function removeHTMLTags(htmlString){
        if(htmlString){
          var mydiv = document.createElement("div");
           mydiv.innerHTML = htmlString;
 
            if (document.all// IE Stuff
            {
                return mydiv.innerText;
               
            }   
            else // Mozilla does not work with innerText
            {
                return mydiv.textContent;
            }                           
      }
   }
Posted on Thursday, October 30, 2008 2:53 PM | Back to top


Comments on this post: How to remove HTML Tags from a string in Javascript

# re: How to remove HTML Tags from a string in Javascript
Requesting Gravatar...
wow what a wonderful way you described it really helps me alot thanks mr agha usman keep doing this sort work for helping more pplz :)
Left by yasir on Oct 30, 2008 3:00 PM

# re: How to remove HTML Tags from a string in Javascript
Requesting Gravatar...
thanks usman,very valuable
Left by Syed Safiullah Hussaini on Oct 30, 2008 3:27 PM

# re: How to remove HTML Tags from a string in Javascript
Requesting Gravatar...
Brilliant! how did you think of it?

can't believe that such a short piece of code can do so much!
Left by rene on Feb 27, 2009 11:51 AM

# re: How to remove HTML Tags from a string in Javascript
Requesting Gravatar...
Hi This is very good code which will other. i am having one dought it will work fine for the word which we typed or for the html tags which are ther but when i copy from the microsoft word than it is giving me diffrent result.
Waiting for ur reply
Thanks in advance.
Left by sabir on Apr 06, 2009 1:03 PM

# re: How to remove HTML Tags from a string in Javascript
Requesting Gravatar...
This is really a great piece of code.it's solved my lot of problems
Left by amitabh on Apr 07, 2009 3:20 PM

# re: How to remove HTML Tags from a string in Javascript
Requesting Gravatar...
it's good , it work me also thanks for the help
Left by narayana on Oct 19, 2009 9:28 PM

# re: How to remove HTML Tags from a string in Javascript
Requesting Gravatar...
this is super. thx
Left by marian on Nov 23, 2009 3:56 AM

# re: How to remove HTML Tags from a string in Javascript
Requesting Gravatar...
this is also very helpfull code, amazing
Left by Rashid on Feb 15, 2010 10:50 PM

# re: How to remove HTML Tags from a string in Javascript
Requesting Gravatar...
Very nice...Thank you for the gift.
Left by Ron on May 06, 2010 8:43 AM

# re: How to remove HTML Tags from a string in Javascript
Requesting Gravatar...
Simple and effective, thank you for this code!
Left by Odi on May 19, 2010 7:23 PM

# re: How to remove HTML Tags from a string in Javascript
Requesting Gravatar...
Very useful
Left by SMP on May 25, 2010 4:51 AM

# re: How to remove HTML Tags from a string in Javascript
Requesting Gravatar...
In Dojo, you can use:

dojo.require("dojox.dtl.filter.htmlstrings");
var foo=dojox.dtl.filter.htmlstrings.striptags(value);
Left by Obro on May 25, 2010 5:09 PM

# re: How to remove HTML Tags from a string in Javascript
Requesting Gravatar...
this is a bad idea to give "myDiv" an html source... it can consist of javascript. and that will be executed during passing html value to div.
Left by Raj... on May 30, 2010 2:02 AM

# re: How to remove HTML Tags from a string in Javascript
Requesting Gravatar...
This works well but doesn't preserve newline information, e.g. if you have two paragraphs one after the other, the output will be the text concatenated instead of being separated.
Left by Jake on Jun 25, 2010 5:47 AM

# re: How to remove HTML Tags from a string in Javascript
Requesting Gravatar...
brilliant piece of code.... :) Thanks dude
Left by Jayesh Sengal on Jul 01, 2010 2:48 AM

# re: How to remove HTML Tags from a string in Javascript
Requesting Gravatar...
This is just brilliant! thanks
Left by Aaron on Jul 22, 2010 6:35 AM

# re: How to remove HTML Tags from a string in Javascript
Requesting Gravatar...
Hey , thanx a lot, you have solved a big problem of mine
Left by Dora on Jul 28, 2010 10:37 PM

# re: How to remove HTML Tags from a string in Javascript
Requesting Gravatar...
How to achieve this using JQuery?
Left by Pit on Jan 23, 2011 8:04 AM

# re: How to remove HTML Tags from a string in Javascript
Requesting Gravatar...
Excellent Article..
Thank u very much..keep posting.
Left by Nani on Sep 28, 2011 11:56 PM

# re: How to remove HTML Tags from a string in Javascript
Requesting Gravatar...
function(s, e) {
var count=0;
var text = s.GetHtml().toString();
text = text .replace(/<.*?>/g,'');
text = text.replace(/(&nbsp;)*/g,'');
count = text.length;
lblResult.SetText(count);
}
Left by rajesh on Mar 19, 2012 12:29 AM

# re: How to remove HTML Tags from a string in Javascript
Requesting Gravatar...
dude,u r a life savior..thanks a lot
Left by karan on Jun 27, 2012 7:20 PM

# re: How to remove HTML Tags from a string in Javascript
Requesting Gravatar...
Hi dude,Simply superb. Thanks a lot
Left by Samsi on Sep 13, 2012 2:19 AM

# re: How to remove HTML Tags and javascript from
Requesting Gravatar...
How to remove html and java script form submit.

html page like

<body>
<script type="text/javascript">
window.alert("My name is George. Welcome!")
</script>
<p>prakash</p>
</body>


output : prakash
Left by prakash on Dec 22, 2013 10:38 PM

Your comment:
 (will show your gravatar)


Copyright © Agha Usman Ahmed | Powered by: GeeksWithBlogs.net