Weird Thoughts From Eric's Head

Tags - Categories : All | AJAX | BUSINESS | PERSONAL | PROGRAMMING | BOOK REVIEW

A little smirk
One day a secretary is leaving on her lunch break, and she notices her boss standing in front of a shredder with a clueless look on his face. The secretary walks up to him and asks if he needs help.

"Yes!" he says looking and sounding relieved, "This is very important."

Glad to help, she turns the shredder on and inserts the paper. Then her boss says, "Thanks, I only need one copy."

Create function like innerText
As you may have figured out innerText is IE only. That means that browsers like Mozilla, Firefox, and Netscape will return undefined. If you do not know what innerText does, it strips out all of the tags so you only see the text.

For example, if a div contains the HTML <span id='span1'>Eric</span>, innerHTML would return <span id='span1'>Eric</span> while innerText will return Eric.

Now to make innerHTML act the same we need to use some regular expressions with the strings replace() method.

Now the basic pattern we need to match is or or or

Now the regular expression we need to use is /<\/?[^>]+>/gi

If you do not know regular expressions here is a quick explanation:

  • / - Starts the regular expression
  • < - Match the less than sign
  • \/ - Escape the character / so it can be matched (Without the \ you would be saying it is the end of the reg exp.)
  • ? - Match the / character 0 or 1 times
  • [^>] - Match any character but greater than sign
  • + - Match [^>] one or more times
  • > - Match greater than sign
  • / - End the regular expression
  • gi - Tells regular expression to match global and ignore the case
So now the function to replace the text would look like:
    <script type="text/javascript">
      var regExp = /<\/?[^>]+>/gi;
      function ReplaceTags(xStr){
        xStr = xStr.replace(regExp,"");
        return xStr;
      }
    </script>
All you need to do is pass it a string and it returns the string stripped of the tags.

An example is shown below to grab the text from a div without the tags.
<html>
  <head>
    <script type="text/javascript">
      var regExp = /<\/?[^>]+>/gi;
      function ReplaceTags(xStr){
        xStr = xStr.replace(regExp,"");
        return xStr;
      }
    </script>
  </head>
  <body>
    <div id="test">
      <span id="span1">Test <u><b>Test</b></u> Test <br/><a href="#">Wow</a>!</span>
    </div>
    <script type="text/javascript">
      var xContent = document.getElementById("test").innerHTML;
      var fixedContent = ReplaceTags(xContent);
      alert(fixedContent);
    </script>
  </body>
</html>
Hope this helps

Eric Pascarello
Moderator of HTML/JavaScript at www.JavaRanch.com
Author of: JavaScript: Your Visual Blueprint for Dynamic Web Pages


I am getting a lot of emails about how they have problems with this code since they use > and < characters improperly. Easly solution is to ecaspe them but you can look at another solution. Another blog: http://www.hanselman.com/blog/AJavaScriptImplementationOfInnerTextNotInnerHtmlForFireFoxAndNonIEBrowsers.aspx has more information on this topic.
Eric
A better way to do this now is: if(document.all){ document.getElementById('element').innerText = "my text"; } else{ document.getElementById('element').textContent = "my text"; }


Add a comment

Title
Body
HTML : b, i, blockquote, br, p, pre, a href="", ul, ol, li
Math Quiz 8 + 10 = (Helps stop blog spam)
Name
E-mail address
Website
Remember me Yes  No 

E-mail addresses are not publicly displayed, so please only leave your e-mail address if you would like to be notified when new comments are added to this blog entry (you can opt-out later).

TrackBack to http://radio.javaranch.com/pascarello/addTrackBack.action?entry=1105721395000