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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:57:45+00:00 2026-06-14T07:57:45+00:00

Sometimes I run into situations where I’m having to create the same variables ,

  • 0

Sometimes I run into situations where I’m having to create the same variables, and retrieve the exact same type of information over & over again while inside of a object literal, such as an .on() Out of sheer curiosity, and the fact that there has to be a better way, here I am.

NOTE I am not talking about jQuery .data() or any sort of normal window. global variable. I am talking one that is maintained within the closure of the object literal.

Some of these variables change in real-time of course, hence why I always had them inside of each method within .on()

Case in point:

$(document).on({
    focusin: function () {
        var placeHolder = $(this).attr('data-title'),
            status      = $(this).attr('data-status');
        // etc etc
    },
    focusout: function () {
        var placeHolder = $(this).attr('data-title'),
            status      = $(this).attr('data-status');
        // etc etc
    },
    mouseenter: function () {
        // same variables
    },
    mouseleave: function () { }
}, '.selector');

Is there a way to just have the variables stored somewhere, and retrieve on each event? They need to be dynamic

$(document).on({
     // Basially:

     // var placeHolder; etc
     // each event gets these values

     focusin: function () {
         // now here I can simply use them
         if (placeHolder === 'blahblah') {} 
         // etc
     }
}, '.selector');
  • 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-14T07:57:46+00:00Added an answer on June 14, 2026 at 7:57 am

    You can use event data to pass some static variables to the event, as well as to make a method-wise trick to pass the “dynamic” ones:

    $(document).on({
        focusin: function(e) {
            var attrs = e.data.getAttributes($(this)),
                var1 = e.data.var1;
    
            var placeHolder = attrs.title,
                status = attrs.status;
            // ...
        },
        focusout: function(e) {
            var attrs = e.data.getAttributes($(this)),
                var1 = e.data.var1;
    
            var placeHolder = attrs.title,
                status = attrs.status;
            // ...
        },
        // ...
    }, ".selector", {
        // static variables
        var1: "some static value",
    
        // dynamic "variables"
        getAttributes: function($this) {
            return {
                title: $this.data("title"),
                status: $this.data("status")
            };
        }
    });
    

    DEMO: http://jsfiddle.net/LHPLJ/

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

Sidebar

Related Questions

I have a fairly complicated hierarchy of types and sometimes run into hard-to-debug type
Has anyone ever run into this issue before? Sometimes the string displays, sometimes half
Sometimes in Salesforce tests you need to create User objects to run part of
I sometimes run into the problem of labels in TreeForm being unreadable because of
I sometimes run into the following scenario : I call View.post(Runnable r) and it
Sometimes I'm on one of my sites, and I run into a DB error
I sometimes run into Fatal error: require() [<a href='function.require'>function.require</a>]: apc_fcntl_lock failed errno:6 in C:\web\yii\framework\web\CWebApplication.php
It may sound stupid, but sometimes I run into version conflicts between two versions
Sometimes I run into situation where I find that I need to make an
Sometimes you run into code that has extra brace brackets, that have nothing to

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.