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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:34:52+00:00 2026-06-10T03:34:52+00:00

First, I apologize. I know what I want to do, but not what I

  • 0

First, I apologize. I know what I want to do, but not what I should call it or quite how to ask it, so my googling was unfruitful.

I have some animation I’m using to show/hide text. I’m trying to wrap it up all nice in an object, but the way I’m doing it, I have to run some calculation code each time, as I don’t know which section it is being stored with.

Now, what I hate is that I’m re-running a calculatePositions(entry); function on every pass rather than using a saved value. Trouble is, this will happen on multiple elements so the positions array needs to change. Is there a way save the positions array to a specific DOM element and just calculate it once? Can I attach these functions and properties to DOM elements rather than pass in the entry object every time?

My code:

var theShort = {};

theShort.toggle = function(){
    var positions = new Array;

    function calculatePositions(entry){
        /*
        positions = Nasty calculation code
        */
    }

    function showLong(entry){
        calculatePositions(entry);
        //My toggle code is obviously more complex and uses the positions array.
        //But for simplicity sake, I've omitted it
        $(entry).find('.tsl-theshort').show();
        $(entry).find('.tsl-thelong').hide();
    }

    function showShort(entry){
        calculatePositions(entry);
        $(entry).find('.tsl-theshort').show();
        $(entry).find('.tsl-thelong').hide();
    }

    return {
        init: function (){
            $('.tsl-showLong').click(function(){
                showLong($(this).closest('.entry-content'));
                return false;
            });

            $('.tsl-showShort').click(function(){
                showShort($(this).closest('.entry-content'));
                return false;
            });
        }
    };
}();

jQuery(document).ready(function($){
    theShort.toggle.init();
});
  • 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-10T03:34:54+00:00Added an answer on June 10, 2026 at 3:34 am

    If you’re worried about running the calculation function every time, set up a cache based on the element’s id. If each element has a unique ID in the dom, you could do something like

    var cache = {};
    
    function showLong(entry){
        var id = $(entry).attr('id');
        if(!cache[id])
            cache[id] = calculatePositions(entry);
    
        //My toggle code is obviously more complex and uses the positions array.
        //But for simplicity sake, I've omitted it
        $(entry).find('.tsl-theshort').show();
        $(entry).find('.tsl-thelong').hide();
    }
    
    function showShort(entry){
        var id = $(entry).attr('id');
        if(!cache[id])
            cache[id] = calculatePositions(entry);
    
        $(entry).find('.tsl-theshort').show();
        $(entry).find('.tsl-thelong').hide();
    }
    

    Then, when you wish to lookup the calculation of an element,

    var id = $(element).attr('id');
    var calculation = cache[id];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First I want to apologize for such a silly question but I'm quite new
First of all, apologize because I have seen some posts about this, but I
First of all I want to apologize for my bad English. I have a
I apologize if this was asked somewhere else, but I do not know how
First off, I want to apologize beforehand, as I know there is a lot
I would like to apologize at first instant for asking some stupid question(well not
First of all, I apologize for the English, I don't know how to write
I apologize for making my first question not the hard-hitting code-related question I was
First, let me apologize if this is the world's stupidest question. But, I'm stumped
First, apologies,this should be simple but I've had too much coffee and cannot wrap

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.