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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:00:34+00:00 2026-05-12T22:00:34+00:00

i am trying not to repeat the selector and get to its children via

  • 0

i am trying not to repeat the selector and get to its children via a the same objects parentElment declared variable.

I tried:

testimonialsBelt={
    parentElment:$(".testimonialsCntnr ul"),
    childrenElem:this.parentElment.children().length
}

I also tried:

testimonialsBelt={
    parentElment:$(".testimonialsCntnr ul"),
    childrenElem:$("testimonialsBelt.parentElment").children().length
}

but i keep on getting a undefined when calling alert(testimonialsBelt.childrenElem).

  1. is there anyway to get the jquery object with object literals?
  2. What is the rule? when can i use this and when must i have the full path? (in this case testimonialsBelt.parentElment).

i am trying to have all these variables in one object called testimonialsBelt. i know i can do this with loose javaScript.
Thanks

  • 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-05-12T22:00:34+00:00Added an answer on May 12, 2026 at 10:00 pm

    In object literals, you can only use this to refer to the object that you’re declaring inside of a function. Try the following:

    var testimonialsBelt = {
        parentElment: $(".testimonialsCntnr ul"),
        childrenElem: function() {
            return this.parentElment.children().length;
        }
    };
    

    The difference in calling childrenElem is that instead of using alert(testimonialsBelt.childrenElem), you would instead have alert(testimonialsBelt.childrenElem()).

    Otherwise, this refers to the current scope that you are in (typically window if you are declaring the object literal as a global).

    Addressing your edit: I’m not sure what you mean by “loose javascript,” but I assume you mean as simple as possible. In which case, you can try the following, although I’m not a big fan of the method. It’s more verbose, but is easy to understand.

    var testimonialsBelt = {
        parentElment: $(".testimonialsCntnr ul")
    };
    testimonialsBelt.childrenElem = parentElment.children().length;
    

    This gives you an object where childrenElem is static (it doesn’t change) and avoids calling $(".testimonialsCntnr ul") twice. However, if you expect $(".testimonialsCntnr ul").children() to change, then you will need to use my first example.

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

Sidebar

Related Questions

I'm trying to not repeat code, so I have a method that does a
Trying not to repeat myself (to be DRY) here, help me out. =) I
The :not always gives me such headache. I am trying to not repeat an
I'm trying to post a form via jQuery .post() , and it's not working
I'm trying to not repeat background. Here is my code $(body).css({background : url(../img/bgr.png), backgroundRepeat:
I am building a site for my professor and am trying not to repeat
Trying not to lose it here. As you can see below I have assigned
require('fortunes') fortune('106') Personally I have never regretted trying not to underestimate my own future
I have two classess and a Userform. I am trying not to use any
I am not trying to create a BOOT_COMPLETED receiver (and doing so would not

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.