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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:22:22+00:00 2026-06-07T12:22:22+00:00

Trying to sort out some general things considering usage of js and jquery particularly.

  • 0

Trying to sort out some general things considering usage of js and jquery particularly.
Was reading ~million of answers through stackoverflow without clean understanding how to pass data to another function and how do we build applications.

I got pretty usual schema:

$(document).ready(function){

   $('.classname').click(function() { 
      var varone = $(this).attr('name');
   });

   $('.class2name').click(function(){
      var vartwo = $(this).height();
   });

});

Then i go out from (document).ready and want to do some handling like

    function dosmth() {
       var freaksum = varone + vartwo;
    }

How do i do that? I want to pass objects, not make globals.

What are the practices to handle such things developing large applications?
I want to have some function which is making lot of operations with lot of variables from other functions. How do i get all these objects in my operational function?
What if i have to use these variables not inside a single function, but in several different functions, like

function dosmth() {
   var freaksum = varone + vartwo;
}

function dosmthelse() {
   var freakmult = varone * vartwo;
}

etc etc

Sorry for a long one, thank you.

  • 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-07T12:22:23+00:00Added an answer on June 7, 2026 at 12:22 pm

    In your current example, your two functions dosmth and dosmthelse do not have any access to the variables varone and vartwo because they do not exist in the same scope. The onlyOne way around this is to make those variables exist in a scope that is available to both the click events and the functions.

    The easiest way is to simply make them global.

    var varone, vartwo;
    
    $(document).ready(function){
       $('.classname').click(function() { 
          varone = $(this).attr('name');
       });
    
       $('.class2name').click(function(){
          vartwo = $(this).height();
       });
    });
    function dosmth() {
       var freaksum = varone + vartwo;
    }
    
    function dosmthelse() {
       var freakmult = varone * vartwo;
    }
    

    However it is usually better to not use the global scope, so we can instead do it this way:

    (function($){
        var varone, vartwo;
        $(document).ready(function){
           $('.classname').click(function() { 
              varone = $(this).attr('name');
           });
    
           $('.class2name').click(function(){
              vartwo = $(this).height();
           });
        });
        function dosmth() {
           var freaksum = varone + vartwo;
        }
    
        function dosmthelse() {
           var freakmult = varone * vartwo;
        }
    })(jQuery);
    

    Now you don’t have any global variables and they are still accessible in both places.

    You could also (depending on your situation) store those values somewhere else, such as on an element.

    Without more information on the real-world situation you are trying to solve, that’s about as far as I can go.

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

Sidebar

Related Questions

I'm debugging this site and trying to sort out some issues that arise in
What I'm trying to find out is whether there is some sort of equivalence
I am currently trying to sort out some logic to check an array for
I'm trying to sort out some homework regarding repetition with the while statement. The
we're trying to sort out some conventions for handling dependencies with code checked into
I've been trying to sort out some of our code using Dispose properly in
Spent a solid hour trying to sort out why on earth this (coffeescript) $.ajax
I've spent days now trying to sort out my deps in Symfony 2.0.15. I
Been trying to sort this one out for a while. I'd really appreciate any
I am trying to figure out how to sort the XML list of employees

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.