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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:02:42+00:00 2026-05-14T18:02:42+00:00

I am writing a bit of code that appends my site nav with and

  • 0

I am writing a bit of code that appends my site nav with and extra ul that gives a description about where that link takes you. I need to use our CMS’s built in Nav structure so appending via jQuery was the best solution, and XML makes the data easier to manage. My question is this: is there a more efficient way to write out the js? What I have so far is this:

$(document).ready(function()
{
 $.ajax({
  type: "GET",
  url: "/js/sitenav.xml",
  dataType: "xml",
  success: function  parseXml(xml) {
  // WORK
   $(xml).find("CaseStudies").each(function()
    {
     $("li#case_studies").append('<ul><li>' + $(this).find("NavImage").text() + $(this).find("NavHeader").text() + $(this).find("NavDescription").text() + $(this).find("NavLink").text()  + "</li></ul>");
    });
   };
 });
});

and the xml structure resembles this:

<SiteNav>
 <Work>
        <CaseStudies>
            <NavImage></NavImage>
         <NavHeader></NavHeader>
         <NavDescription></NavDescription>
         <NavLink></NavLink>
        </CaseStudies>
    </Work>
</SiteNav>

I’m happy with my xml structure, but is there a more compact/efficient method of writing out the code for the jQuery? Every li in the nav has a unique id as well in case that helps…

  • 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-14T18:02:43+00:00Added an answer on May 14, 2026 at 6:02 pm

    Since you’re just appending the text in the same order as it occurs in the xml document, you can do this:

    var cs = $("li#case_studies"); //Cache the <li> reference
    $(xml).find("CaseStudies").each(function() {
      cs.append('<ul><li>' + $(this).children().text() + "</li></ul>");
    });​
    

    This caches the reference to the li#case_studies so you’re not traversing the DOM each time finding it. Also you’re doing 1/4 as many traversals of the XML in the loop since you’re just looking at the child collection once instead of 4 finds.

    This only works as an optimization as long as the order of the elements in the XML matches the order you want, from your question it does, just be aware of that in case that may change.

    You can see an example of the code above working here.

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

Sidebar

Related Questions

I'm having issues with a bit of code that I am writing in C#.
I'm writing a bit of code that I want to populate a TreeView, which
I'm writing a bit of code that will open a MessageBox on a separate
I'm current writing a short bit of code that will compare an etag for
So I'm writing a bit of code that needs to raise a function's return
I am writing a bit of code to add a link tag to the
I was just writing a quick bit of code, and I wanted to use
I am writing bit of code that relies on XslTransformation of one submodule. Since
I'm am writing a bit of python code that gets data from a website.
I am writing a bit of code that implements an (unsigned) integer counter. It

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.