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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:25:43+00:00 2026-06-05T12:25:43+00:00

Currently tying to keep my code within an object, I would like to convert

  • 0

Currently tying to keep my code within an object, I would like to convert the contents of a click event into a method but not overly sure how this is done. The current code looks like this:

Current JS

init: function(){

            var _this = this,
                slides_li = _this.els.slides.children(),
                large = 260,
                small = 60;

            // Hover
            slides_li.on('mouseover', function(){
                $(this).stop(true, true).animate({ opacity: 1 }, 300);
            }).on('mouseout', function(){
                $(this).stop(true, true).animate({ opacity: .8 }, 300)
            });

            // Click handlers
            slides_li.on('click', function(){

// Would like to move this code into its own method toggle_slides() ??

                $('.active', _this.els.slides).not(this).animate({
                    width: small
                }, 300).removeClass('active');
                // animate the clicked one

                if ( !$(this).hasClass('active') ){
                    $(this).animate({
                        width: large
                    }, 300).addClass('active');
                }                
            });                 
        }

but I would like the code to look like this but I know I’m missing a few key things plus this obviously doesn’t mean the clicked event:

JS

init: function(){

            var _this = this,
                slides_li = _this.els.slides.children(),
                large = 260,
                small = 60;

            // Hover
            slides_li.on('mouseover', function(){
                $(this).stop(true, true).animate({ opacity: 1 }, 300);
            }).on('mouseout', function(){
                $(this).stop(true, true).animate({ opacity: .8 }, 300)
            });

            // Click handlers
            slides_li.on('click', function(){
                toggle_slides(); //pass in this?
            });                 
        },
        toggle_slides: function(){ // add argument for this?
               $('.active', _this.els.slides).not(this).animate({
                    width: small
                }, 300).removeClass('active');
                // animate the clicked one

                if ( !$(this).hasClass('active') ){
                    $(this).animate({
                        width: large
                    }, 300).addClass('active');
                }                
        }

Can anyone offer some advice on how to make this work?

  • 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-05T12:25:45+00:00Added an answer on June 5, 2026 at 12:25 pm

    The problem is the ever-context-dependent value of ‘this’.

    First, a note about how the original code worked:

    When init() is originally called, this refers to the parent object (gep). But within the click event handler, this refers to the clicked element. So that you can still access the parent within the click handler, you capture the ‘parent value of this’ into _this, so you can use it for later – and everything works fine.

    But when you move the code in the handler into a separate method, quite a few things change:

    • First, small, large and other variables are no longer in the local scope, so have to be either redefined or imported as parameters.

    • this now refers to the parent element again, because the method now executing is not the event handler, but a method on the parent element. So references to _this in the old code can just use this in the new code.

    • Finally, in the old code, in the event handler, this referred to the element that was clicked. But (see above) in the new method, this means something different: the ‘parent’ object. So we need a parameter to capture that clicked element – I’ve passed it as an el parameter, and references to this in the old code change accordingly.

    So the real thing to watch for is: what object does this code belong to? If you move code belonging to one object to another – eg. from an event handler on one object to a method on another object – you’ll likely need to rework/rename any this or this-like variables as appropriate.

    Annotated copy of the updated code follows, also available as a jsfiddle:

    ...
            // Click handlers
            slides_li.on('click', function(){
                // pass 'this' - the clicked element - as the el param; also small and large.
                gep.toggle_slides(this, small, large);
            });                 
        },
    
        toggle_slides: function(el, small, large){
               // '_this' in old code in handler replaced with 'this';
               // 'this' in handler code replaced with 'el' here
               $('.active', this.els.slides).not(el).animate({
                    width: small
                }, 300).removeClass('active');
                // animate the clicked one
    
                // 'this' in handler code replaced with 'el'here
                if ( !$(el).hasClass('active') ){
                    $(el).animate({
                        width: large
                    }, 300).addClass('active');
                }                
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Some sample, straight forward gtk2 within-main-loop I'm currently trying to add code at key-press-event
Im trying to create an indexed 2D array within Python, but I keep running
I'm currently struggling to use UI elements in Interface Builder. I keep trying to
I'm currently implementing a user registration for an app. I'm tying to change the
My employer is currently trying out Fogbugz and one feature that would be nice
The following code is within an ajax call. I'm trying to make sure people
I currently have a program where my main code is in a file main.cpp.
I am currently trying to build some test code that uses Google C++ Test
i am currently using this line of code resourceManager = new ResourceManager(Namespace.class, GetType().Assembly); to
I know of at least two byte-code enhancer that modify the object model at

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.