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

  • Home
  • SEARCH
  • 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 8349941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:09:40+00:00 2026-06-09T08:09:40+00:00

I am trying to loop through all elements in a given div and output

  • 0

I am trying to loop through all elements in a given div and output the results (C# code i will use later) to the screen for testing.

so if i have html like this:

    <div id="testDiv">
        <test>
            <a>aVal</a>
            <c>
                <cc>ccVal</cc>
            </c>
       </test>
    </div>

i am trying to produce this string value:

HtmlElement.CreateNode("test").AddNode(CreateNode("a").addText("aVal")).AddNode(CreateNode("c").AddNode(CreateNode("cc").addText("ccVal"))

Right now i ahve this jquery in place, but i am unsure of how to drill down into the other nodes:

var x = "HtmlElement.";
$('div#testDiv').children().each(function () {    
    var nodeNameStr = this.nodeName.toLowerCase();
    var nodeText = $(this).text();
    x += "CreateNode(nodeNameStr).addText(nodeText)"
});
  • 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-09T08:09:42+00:00Added an answer on June 9, 2026 at 8:09 am

    Here’s a more complete example than previous answers:

    http://jsfiddle.net/4QtS5/

    // returns the 'AddNode(...)' method call for every child.    
    function addChildren(element){
       var command = "";
       $(element).find("> *").each(function(){
          command += ".AddNode("+createNode(this)+")";
       });
       return command;
    }
    
    // if the element has text, add the text
    function addText(element){
       var elementText = $(element).clone().children().remove().end().text().trim();
       if(elementText) {
          return ".addText(\""+elementText+"\")";
       } else {
          return "";
       }
    }
    
    // returns the 'CreateNode(...)' method call for a node and all its children.
    function createNode(element){
       var nodeName =  element.nodeName.toLowerCase();
       var csharpCommand = "CreateNode(\""+nodeName+"\")";
       csharpCommand += addChildren(element);
       csharpCommand += addText(element);
       return csharpCommand;
    }
    
    // begin
    $("div#testDiv > *").each(function(){
        var csharpCommand = "HtmlElement."+createNode(this);
        console.log(csharpCommand);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use Linq to loop through all fonts in the %windir%\Fonts folder
I'm trying to write a unit test that will loop through all action methods
I'm trying to loop through a list of results from MySQL and display all
How can i loop through all label elements in a div using jQuery or
I'm trying to get all form elements to array, then loop through them using
I'm trying to loop through all files of .properties extension, in a root folder:C:\ExecutionSDKTest_10.2.2\,
I'm trying to loop through the results from the Last.fm API but it's not
I am trying to loop through a vectors elements and trying to find out
I'm trying to loop through an object with JavaScript and add all the subobjects
I am trying to loop through some elements in a form (radios, text boxes,

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.