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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:55:36+00:00 2026-06-17T11:55:36+00:00

My code seems flawed, I’m setting two functions with the same code inside. Can

  • 0

My code seems flawed, I’m setting two functions with the same code inside. Can I combine them into one?

// remove sets
$('body').on("click", "span.remove", function() {

    var self  = $(this),
        total = self.parents('ul').children('li').size(); // grab total number of li's

    // remove (set) li if total greater than 1, else remove exercise
    if(total > 1)
        self.parent('li').fadeOut('slow', function() {

            // remove clicked .li and reset list order
            $(this).remove();
            reset();

        });

    else
        self.parents('section').fadeTo('slow', 0.33).slideUp('slow', function() {

            // remove clicked .li and reset list order
            $(this).remove();
            reset();

        });

});

Is it possible to do something like:

if(total > 1)
    var f = self.parent('li').fadeOut('slow', function()

else
    var f = self.parents('section').fadeTo('slow', 0.33).slideUp('slow', function()

f {

    // remove clicked .li and rest list order
    $(this).remove();
    reset();

});
  • 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-17T11:55:37+00:00Added an answer on June 17, 2026 at 11:55 am

    The trick is to create a named function, instead of an anonymous function. A named function can then be passed around by reference, including as a parameter to other methods. There are two ways to do this, either as an expression (function someName(){..}) or an assignment (var someName = function() {..}). Which you choose is mainly personal preference, they are effectively the same.

    An example (using a function expression), following on from your question below:

    $('body').on("click", "span.remove", function() {
    
        function removeAndReset() {
                // remove clicked .li and reset list order
                $(this).remove();
                reset();
        }
    
        var self  = $(this),
            total = self.parents('ul').children('li').size(); // grab total number of li's
    
        // remove (set) li if total greater than 1, else remove exercise
        if(total > 1)
            self.parent('li').fadeOut('slow', removeAndReset);
    
        else
            self.parents('section').fadeTo('slow', 0.33).slideUp('slow', removeAndReset);
    
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a flawed implementation of detecting if two urls are on the same
when printing a page, the javascript code seems to be executed. how can I
This code seems to work well, modifying the properties of my button, except the
The code seems not working. // $counter is an instance of Zend_Db_Table_Abstract $counter->update(array('hits' =>
My code seems to do infinite recursion when I invoke it with trees of
My code seems to run but it does not display the result any result
The following javascript code seems to leak memory but I’m not sure why. I’ve
The following code seems to be just too much, for getting a single count
The below code seems like it should work; however, the blob in the database
The following code seems not to work, even though the file appears to be

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.