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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:28:36+00:00 2026-06-14T10:28:36+00:00

I have a javascript class that has a method that uses jQuery to send

  • 0

I have a javascript class that has a method that uses jQuery to send an Ajax request and handle the response.

The problem I am having is that I can’t figure out how to get the properties of the initial, parent class from within the jQuery functions. I have tried $(this).parent() but this doesn’t get what I need for some reason.

My code is below. Can anyone tell me how to get to the base class from this loop?

function companiesPage()
{
    this.childCategoriesSelectid = '#childCategoryid';

    this.setChildCategories = function()
    {
        $.ajax({
            url: this.url,
            dataType: 'json',
            success: function(data)
            {
                $.each(data.childCategories, function()
                {
                    $($(this).parent().childCategoriesSelectid)//problem here
                    .append(
                        $('<option></option>')
                        .attr('value', this.childCategoryid)
                        .text(this.name)
                    );
                });
            }
        });
    }
}
  • 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-14T10:28:37+00:00Added an answer on June 14, 2026 at 10:28 am

    parent() is for DOM traversal. What you have is a scoping problem.

    The this keyword does not always references the same object when entering a new function scope. That is the case when using jQuery’s .each() method which will set this to the object/element being iterated.

    Looks like you’re using a constructor-instance pattern, so you just have store a reference to the original object that the this keyword referenced to (the instance) through a var declaration. This reference will remain unchangeable in the scope chain:

    function companiesPage()
    {
        var _this = this; //stores a reference to this instance object
        this.childCategoriesSelectid = '#childCategoryid';
    
        this.setChildCategories = function()
        {
            $.ajax({
                url: this.url,
                dataType: 'json',
                success: function(data)
                {
                    $.each(data.childCategories, function()
                    {
                        $(_this.childCategoriesSelectid) //NO problem here :)
                        .append(
                            $('<option></option>')
                            .val(this.childCategoryid)
                            .text(this.name)
                        );
                    });
                }
            });
        }
    }
    

    This way you will be able to access any of your instance’s public methods and properties anywhere inside of your instance.

    ps. Instanceables/Constructors usually have the first letter upper-cased, by convention.

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

Sidebar

Related Questions

I have a JavaScript class that has a method: function someClass() { this.someMethod =
I want to create a Javascript class/object that allow me to have various method:
A Little Background I have written a method in javascript/jQuery that loops through fields
I have some javascript that goes out and fetches a javascript class on another
I have a class that stores paths to CSS and Javascript files in arrays.
I have a function in javascript that changes a span class when I click
I have a span that is generated through javascript, with its css class as
i have a class called light that loads its state by ajax: function Light(name,
I have an Android WebView that has JavaScript that is calling Android methods through
I have defined a class in JavaScript with a single method: function MyClass(text) {

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.