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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:59:29+00:00 2026-05-20T10:59:29+00:00

Code style advice, please: I want to prevent the rampant distribution of selector strings,

  • 0

Code style advice, please:

I want to prevent the rampant distribution of selector strings, especially similar bases, throughout my code.

function fn1() {
  $("#formId ul.sectionClass li.statusFlag").doSomething();
  $("#formId ul.sectionClass li.otherStatusFlag").doSomeOtherThing();
  doSomethingToGroup("#formId ul.sectionClass");
  doSomethingToOtherGroup("#formId ul.otherSectionClass");
}

function doSomethingToGroup(selector) {
  $("#formId>.statusBar").html(summarize(selector));
  $(selector).doMore();
}

function classesLikeIds() {
  $("#formId .item1").doOneThing();
  $("#formId .item2").doAnotherThing();
}

...etc.

Functionally, I’m comfortable my code is fairly DRY. Divisions of responsibility are healthy, etc. But I still have selector strings scattered throughout my code that are difficult to maintain and causing defects.

Possible solution:

I’ve thought about something as simple as a named array of selectors:

AppName.Selectors = {
  form: "#formId",
  statusBar: "#formId .statusBar",
  activeItems: "#formId ul.sectionClass li.statusFlag",
  inactiveItems: "#formId ul.sectionClass li.otherStatusFlag"
}

That seems more maintainable, and a javascript compiler could alert me to many more problems. I still feel like it’s pretty weak, though. If you do this, but have an object model that makes it more intuitive or supports child relations, please post it as a solution.

Maybe my style is part of the problem:

Maybe it is bad or controversial, but I try to minimize unique IDs in my HTML, even sometimes using classes like IDs (beneath top-level element IDs). For example:

//I'll use
$("#appName form .header")
//Rather than
$("#appNameHeader")

Why? If an app has 100 IDs in it, bad stuff happens in my experience. Two quick examples: 1) mashing-up apps becomes fraught with name collision danger, 2) it is harder to intuit the impact of style changes on child elements.

What do you do?

Thanks,
Shannon

  • 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-20T10:59:30+00:00Added an answer on May 20, 2026 at 10:59 am

    I’d suggest that you store the result of the selector in order to be more efficient.

    Elements = {
      form: $("#formId"),
      statusBar: Elements.form.find(".statusBar"),
      sectionClass: Elements.form.find("ul.sectionClass"),
      activeItems: Elements.sectionClass.find("li.statusFlag"),
      inactiveItems: Elements.sectionClass.find("li.otherStatusFlag")
    };
    

    thus you’ll reuse the selector results, which results in better performance. But this may not work if you’ve elements that are being added in the DOM later and match these selectors.

    If you go with the above approach, you might’ve to change your methods/functions to expect an array of elements rather than a string.

    For eg.,

    function doSomethingToGroup(elems) {
      Elements.statusBar.html(summarize(elems.selector));
      elems.doMore();
    }
    

    You can always get the selector string from the cached results using the selector method.
    Elements.statusBar.selector returns the selector #formId .statusBar

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

Sidebar

Related Questions

I have this div in my code: <div id=advice class=validation-advice style=>some text here</div> Now,
in my silverlight application i want to create a style from code behind and
I have a templating system that looks similar to old-style ASP code. I run
The code that I am working with has tons of style that I want
I have an issue with this jquery code below. Please give me advice or
I'm just starting Android development, so I'd like a little advice on code style.
Android Code Style Guide defines Android Code Style Rules. To conform to these rules
For pop up im using the following code style : a.selected { background-color:#1F75CC; color:white;
what is the proper code for this? in div style code. I know how
Is there some tool that can automatically convert the following c style code A

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.