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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:07:55+00:00 2026-06-14T02:07:55+00:00

Possible Duplicate: jquery passing $(this) to other functions I am writing a Javascript/jQuery function

  • 0

Possible Duplicate:
jquery passing $(this) to other functions

I am writing a Javascript/jQuery function (NOT REFACTORED YET) but I am getting an error from using $(this). It’s a little difficult to explain so I’ll add the code and hopefully it will become more clear:

var accordion = 'div.accordionContent';
var accordButton = 'div.accordionButton';
var accordClosedArrow = 'url(/public/img/accordion-closed-arrow-';
var accordOpenArrow = 'url(/public/img/accordion-open-arrow-';
$(accordion).hide();
var accordFunc = function(arrowColor){
    var img;
    if ($(this).next(accordion).is(':visible')) {
        img = accordClosedArrow + arrowColor + '.png)';
    } 
    else {
        img = accordOpenArrow + arrowColor + '.png)';
    }

    // these lines throw errors because of $(this)
    $(this).css({backgroundImage:img}).next().slideToggle('slow').siblings(accordion)
        .slideUp();
    $(this).siblings()
        .css({backgroundImage:accordClosedArrow + arrowColor + '.png)' + 'no-repeat'});
    };

    $(accordButton).live('click',function(){
        accordFunc('blue');
    });

    $(accordButton + 'B').live('click',function(){
        accordFunc('orange');
    });

Not sure why passing $(this) causes the Chrome console to throw an error:

Uncaught TypeError: Cannot read property ‘firstChild’ of undefined

  • 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-14T02:07:56+00:00Added an answer on June 14, 2026 at 2:07 am

    Replace accordFunc('blue'); with accordFunc.call(this, 'blue'); and do likewise for the other call to that function.

    The this value is not preserved when you call another function so inside accordFunc it points to the window object or null (if strict mode is active). Another option would be passing this as a normal argument and changing the function definition e.g. to function accordFunc(elem, arrowColor) and use elem instead of this inside that function.

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

Sidebar

Related Questions

Possible Duplicate: passing variables from php to javascript I am using this awesome jQuery
Possible Duplicate: jQuery .load method not firing on IE9 In IE7 and IE8 this
Possible Duplicate: JavaScript/jQuery HTML Encoding I am passing info down to the client as
Possible Duplicate: Jquery event chaining Traditionally we may write: $(selector).click(function () { }); but
Possible Duplicate: Learning jQuery and Getting Better at Javascript I have found jQuery very
Possible Duplicate: Passing JavaScript Array To PHP Through JQuery $.ajax I'm trying to pass
Possible Duplicate: passing index from for loop to ajax callback function (javascript) I'd like
Possible Duplicate: Bind multiple events to jQuery 'live' method I have the following function:
Possible Duplicate: Passing javascript variable to PHP Hi I wonder if it's possible to
Possible Duplicate: jQuery voting system I've been using this tutorial to create a voting

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.