Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7687459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:42:30+00:00 2026-05-31T19:42:30+00:00

I am attempting to do a quick replace of the ‘innerHTML’ of the ‘code’

  • 0

I am attempting to do a quick replace of the ‘innerHTML’ of the ‘code’ element. I thought this may work:

function codeDisplay ( ) {
    var code = document.getElementsByTagName('code').innerHTML;

    var codeExam1 = new RegExp('<', 'gm');
    var codeExam2 = new RegExp('>', 'gm');

    code.replace(codeExam1, '&lt;');
    code.replace(codeExam2, '&gt;');
      }

Do I need to perform any additional steps to push the information back to the browser or conversion of data types maybe? Or am I completely wrong in how ‘RegEx’ and ‘innerHTML’ work? I appreciate the feedback in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:42:32+00:00Added an answer on May 31, 2026 at 7:42 pm

    So, first fo all:

    var code = document.getElementsByTagName('code').innerHTML;
    

    document.getElementsByTagName returns a list of elements not just one. So, if your purpose is escaping all the code tags you have in the page, you need to iterate them.
    Second, I believe you can avoid regexp just using textContent (where supported) or innerText.

    var codes = document.getElementsByTagName("code");
    
    for (var i = 0, code; code = codes[i++];) {
        if ("textContent" in code)
            code.textContent = code.innerHTML;
        else if ("innerText" in code)
            code.innerText = code.innerHTML;
    }
    

    or create a new text node:

    var codes = document.getElementsByTagName("code");
    
    for (var i = 0, code, html; code = codes[i++];) {
        html = code.innerHTML;
    
        code.innerHTML = "";
    
        code.appendChild(document.createTextNode(html));
    }
    

    That’s should escape every html entities. If you still want to use the regexp, maybe as fallback, you can have this kind of function:

    var escapeEntities = (function(){
        var entities = {"<" : "lt", ">" : "gt", "&" : "amp" };
        var re = new RegExp("[" + Object.keys(entities).join("") + "]", "g");
    
        function replaceEntities(match) {
            return match in entities ? "&" + entities[match] + ";" : match;
        }
    
        return function(value) {
            return value.replace(re, replaceEntities);
        }
    })()    
    

    And then in your code:

    code.innerHTML = escapeEntities(code.innerHTML);
    

    Note that if Object.keys is not supported you can easily use a shims (as indicated in the link); or simply replace manually the list of entities you support:

     var entities = {"<" : "lt", ">" : "gt", "&" : "amp" };
     var re = /[<>&]/g;
    

    In that case you need to remember to add in both entities and re variables a new entity you want to support in the future; Object.keys just help you in maintenance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Quick facts, I got this function from http://lua-users.org/wiki/SplitJoin at the very bottom, and am
Attempting to use the data series from this example no longer passes the JSONLint
Is there a more comprehensive quick start for drools 5. I was attempting to
Alright, here's what I'm trying to do. I'm attempting to write a quick build
Edit: This may be a 5.0-specific bug. (I'm on 5.0.83). Removing the innodb_log_file_size setting
Quick example: An error occurred when attempting to save a record to a db.
I want to create a quick function that will console.log a variable name and
So, here's the deal. I am attempting to write a quick python script that
This is my first experience using the Zend Framework. I am attempting to follow
I am new to Zend and have been attempting to follow the Zend Quick

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.