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 8043099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:48:10+00:00 2026-06-05T04:48:10+00:00

I have the following piece of code (for the present case it may be

  • 0

I have the following piece of code (for the present case it may be considered as a function to remove the attributes from a valid html string fed as input):

function parse(htmlStr)
{
console.log(htmlStr);
result+="<"+htmlStr.tagName.toLowerCase()+">";
var nodes=htmlStr.childNodes;
for(i=0;i<nodes.length;i++) {
    var node=nodes[i];
    if(node.nodeType==3) {
        var text=$.trim(node.nodeValue);
        if(text!=="") {
            result+=text;
        }
    }
    else if(node.nodeType==1) {
        result+=parse(node);
    }
}
result+="</"+htmlStr.tagName.toLowerCase()+">";
return result;
}

But it is not working as expected. For example, in the following case when I feed it the following html as input:

<div id="t2">
    Hi I am
    <b>
      Test
    </b>
</div>

it returns <div>Hi I am<div>Hi I am<b>Test</b></div>.

Also the page crashes if some large input is given to the function.

NOTE: I know there are better implementations of removing attributes from a string using jQuery, but I need to work with the above function here & also the complete code is not for removing attributes, the above is just a shortened part of the code

  • 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-06-05T04:48:12+00:00Added an answer on June 5, 2026 at 4:48 am

    There is something wrong with your result variable. It is undefined and global. In each recursion you would append the same string to itself, which also makes it crashing for huge inputs. (I can’t reproduce anything, it crashes right away with a Undefined variable Error)

    BTW: Your argument is no htmlStr, it is a domNode. And you’re not parsing anything. Please don’t use wrong self-documenting variable names.

    Corrected version:

    function serialize(domElement) {
        var tagname = domElement.tagName.toLowerCase();
        var result = "<"+tagname+">";
    //  ^^^       ^ not a +=
        var children = domElement.childNodes;
        for (var i=0; i<children.length ;i++) {
    //       ^^^ was also missing
             if (children[i].nodeType == 3) {
                 result += children[i].data;
             } else if (children[i].nodeType == 1) {
                 result += serialize(children[i]);
    //                  ^^ add a child's result here
             }
        }
        result += "</"+tagname+">";
        return result;
    }
    

    I would not use trim(), that would produce <div>Hi<b>I</b>am</div> from <div>Hi <b>I</b> am</div>. You might do something like .replace(/\s+/g, " ").

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

Sidebar

Related Questions

I have following piece of code : public Hashmap<String,String> tempmap = new HashMap<String,String>(); and
I have the following piece of code that is taken from a mock exam
I have the following piece of code taken from the PHP manual on the
I have following piece of code which tries to load an XML file from
I have the following piece of code: jQuery.fn.shake = function(intShakes, intDistance, intDuration) { this.each(function()
I have the following piece of code: actor { loop { react { case
I have this following piece of code: public TimestampedRowStorage GetTimestampedRowStorage(string startTime, string endTime, long
I have the following piece of code: var blah = function(x, y){ var e
I have following piece of code: byte[] snapthotBytes, snapthotBytes2; string stringOutput, stringOutput2; IInvestigationDump investigationDump
I have the following piece of code pattern: void M1(string s, string v) {

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.