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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:03:15+00:00 2026-06-15T20:03:15+00:00

I have the following use of jQuery UI function: $(‘.droppable’).droppable({ tolerance: ‘touch’, over: function

  • 0

I have the following use of jQuery UI function:

$('.droppable').droppable({
    tolerance: 'touch',
    over: function () {
        var hasHiddenList = $(this).children('ul.hidden');
        if (hasHiddenList.length)
            hasHiddenList.removeClass('hidden');
    },
    out: function () {
        var hasEmptyList = $(this).children('ul.empty');
        if (hasEmptyList.length)
            hasEmptyList.addClass('hidden');
    },
    drop: function () {
        var hasEmptyList = $(this).children('ul.empty');
        if (hasEmptyList.length)
            hasEmptyList.removeClass('empty');
    }
});

And I am wondering if I can define the variables hasHiddenList and hasEmptyList outside the callback functions, since it is the same variable in all of them.

  • 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-15T20:03:17+00:00Added an answer on June 15, 2026 at 8:03 pm

    If you really, really want the variables, there are two options.

    The obvious one (with pitfalls) is this:

    var hasHiddenList = $('.droppable').children('ul.hidden');
    var hasEmptyList = $('.droppable').children('ul.empty');
    
    $('.droppable').droppable({
        tolerance: 'touch',
        over: function () {
            if (hasHiddenList.length)
                hasHiddenList.removeClass('hidden');
        },
        out: function () {
            if (hasEmptyList.length)
                hasEmptyList.addClass('hidden');
        },
        drop: function () {
            if (hasEmptyList.length)
                hasEmptyList.removeClass('empty');
        }
    });
    

    Variables have been extracted, the code is less repetitive. All good.

    Now, the problem is that the selectors are just run once. The callback over, out and drop are called every time one of those events occur, but hasHiddenList and hasEmptyList will not have been updated; they will still be the same thing between every invocation. Probably not what you want.

    The way to remedy this is to use functions instead of variables, like this:

    var hiddenList = function(target) {
        return $(target).children('ul.hidden');
    };
    var emptyList = function(target) {
        return $(target).children('ul.empty');
    };
    
    $('.droppable').droppable({
        tolerance: 'touch',
        over: function () {
            var list = hiddenList(this);
            if (list.length)
                list.removeClass('hidden');
        },
        out: function () {
            var list = emptyList(this);
            if (list.length)
                list.addClass('hidden');
        },
        drop: function () {
            var list = emptyList(this);
            if (list.length)
                list.removeClass('empty');
        }
    });
    

    This abstracts away how the queries are performed (you only have to write $(target).children('ul.empty'); once instead of twice) but in reality the code has not really become simpler. Actually, I would even say that it’s harder to follow it now. On the other hand, if you were using the same selector even more than twice, lets say five times, then maybe this would actually be useful.

    Also, note that I’ve removed “has” from the variable names, because “has” makes it sound like they are booleans, which they are not. They are lists of elements.

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

Sidebar

Related Questions

I have the following html where I use the jquery after method() to stick
I have the following line of jQuery but it seems like overkill to use
I have the following code: use strict; function isDefined(variable) { return (typeof (window[variable]) ===
I have following Image as src in ImageView. I use this as background image.
I have the following piece of code: function checkAmount() { var PayField = document.getElementById('paymentamount');
I have the following page: <html> <head> <title>Test</title> <script type=text/javascript> function BuildTree() { var
I have the following html and I want to use jQuery to toggle the
Let's say we have the following JavaScript/jQuery code below function createElement(i, value) { return
I have the following jQuery function which is supposed to detect when an element
I have the following jQuery function: function GetGrandparentDiv(item) { return item.parents('div:eq(1)'); } Which is

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.