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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:50:18+00:00 2026-05-15T08:50:18+00:00

Suppose you have a Django view that has two functions: The first function renders

  • 0

Suppose you have a Django view that has two functions:

The first function renders some XML using a XSLT stylesheet and produces a div with 1000 subelements like this:

<div id="myText">
    <p id="p1"><a class="note-p1" href="#" style="display:none" target="bot">✽</a></strong>Lorem ipsum</p>
    <p id="p2"><a class="note-p2" href="#" style="display:none" target="bot">✽</a></strong>Foo bar</p>
    <p id="p3"><a class="note-p3" href="#" style="display:none" target="bot">✽</a></strong>Chocolate peanut butter</p>
     (etc for 1000 lines)
    <p id="p1000"><a class="note-p1000" href="#" style="display:none" target="bot">✽</a></strong>Go Yankees!</p>
</div>

The second function renders another XML document using another stylesheet to produce a div like this:

<div id="myNotes">
    <p id="n1"><cite class="note-p1"><sup>1</sup><span>Trololo</span></cite></p>
    <p id="n2"><cite class="note-p1"><sup>2</sup><span>Trololo</span></cite></p>
    <p id="n3"><cite class="note-p2"><sup>3</sup><span>lololo</span></cite></p>
     (etc for n lines)
    <p id="n"><cite class="note-p885"><sup>n</sup><span>lololo</span></cite></p>
</div>

I need to see which elements in #myText have classes that match elements in #myNotes, and display them. I can do this using the following jQuery:

$('#myText').find('a').each(function() {
    var $anchor = $(this);
    $('#myNotes').find('cite').each(function() {
        if($(this).attr('class') == $anchor.attr('class')) {
            $anchor.show();
    });
});

However this is incredibly slow and inefficient for a large number of comparisons.

What is the fastest/most efficient way to do this – is there a jQuery/js method that is reasonable for a large number of items? Or do I need to reengineer the Django code to do the work before passing it to the template?

  • 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-15T08:50:19+00:00Added an answer on May 15, 2026 at 8:50 am

    For best-possible performance, make an index once and then re-use it:

    function revealCite() {
      var cites_index = $("#myText").data("cites_index");
    
      // if no cached index exists, prepare one (one-time hit code section)
      if (!cites_index) {
        var cites = $("#myNotes cite");
        var cites_count = cites.length();
        var cites_index = {};
    
        for (var i=0; i<cites_count; i++) {
          var cite = cites[i], group = cites_index[cite.className];
          if (!group) cites_index[cite.className] = [];
          group.push(cite);
        }
        $("#myText").data("cites_index", cites_index);
      }
    
      // use the index to work with related elements ("this" must be an <a> element)
      $(cites_index[this.className]).show();
    }
    

    Now trigger the above function any way you like:

    $("#myText a").each(revealCite);
    

    PS: You could also do this, in place of the for loop:

    cites.each( function() {
      var group = cites_index[this.className];
      if (!group) cites_index[this.className] = [];
      group.push(this);
    });
    

    But it’s the same number lof lines of code, and probably is a bit slower.

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

Sidebar

Ask A Question

Stats

  • Questions 454k
  • Answers 454k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Parse() can't parse null or incorrect input and throws an… May 15, 2026 at 9:50 pm
  • Editorial Team
    Editorial Team added an answer Don't use sp_helplogins: use sys.server_principals SELECT * FROM sys.server_principals WHERE… May 15, 2026 at 9:50 pm
  • Editorial Team
    Editorial Team added an answer how to find out why this solution is so slow.… May 15, 2026 at 9:50 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.