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

  • Home
  • SEARCH
  • 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 8426003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:25:48+00:00 2026-06-10T04:25:48+00:00

I initially began my task by trying to use JS best practices when writing

  • 0

I initially began my task by trying to use JS best practices when writing this code, but I was struggling, so I just wrote it as a big sloppy mess to get it to work, and then tried to clean it up after the fact. (You may argue that that wasn’t a good idea, but it was the best I could do with where I am.)

My boss suggested I try to use custom triggers and events, but that didn’t really seem the best route. I also read this article by Rebecca Murphy on OOJS that seemed like it might have an answer, but I just couldn’t get there.

Keep in mind that, while I’m definitely specifically looking for a more elegant (and less nested-if-statement-dependent) way to write this and definitely appreciate any help on that matter, I’m also looking for patterns that could help me address my larger stylistic shortcomings here and become a better programmer. For example, I know that this is the clunkiest and slowest way to traverse the DOM.

One important note: I can’t control much of the markup (aside from adding classes as JS/CSS hooks) in this situation, because it’s being generated by the SimpleForm Ruby gem (I’m unable to link to it because of my low score). And the CSS is irrelevant. So it’s really just the JS.

Context

Certain things are being shown/hidden based on select or radio button choices.

EDIT: Sorry, my HTML was getting weird here, so I just made a jsFiddle.

EDIT 2: We’re using jQuery 1.6.4, so if you’re wondering why I used .delegate instead of .on, that’s why.

  • 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-10T04:25:49+00:00Added an answer on June 10, 2026 at 4:25 am

    You could also keep things DRY by removing the duplicated code to show/hide items. Here is a very simplistic approach that improves things:

    function changeElements (show_elements, hide_elements) {
        if (show_elements === null) {
            // code to hide all
        }
    
        container.siblings(show_elements).slideDown();
        container.siblings(hide_elements).slideUp();
    }
    

    and then to use the function, simply give it the selectors to show, and to hide respectively:

    changeElements('.deceased', '.living, .other-illnesses, .cancer-info');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this small code just to see how an iterator actually gets invalidated
Initially I had the habbit of directly writing the php code in the <?php
Initially I thought this was going to work, but now I understand it won't
I have this piece of code. The BufferAddress was initially of PByteArray type. However,
I initially asked this question on the libRocket forum, but given that further investigation
Initially I thought it was a CSS issue but I built a small sample
Initially I had designed this flash based navigation menu for my website, after going
Initially I deployed packages on SQL server but since my machine is not having
I have this seemingly-innocent code on my main JSF page: <a4j:outputPanel id=sidebarContainer> <a4j:include viewId=#{UserSession.currentSidebar}/>
I have following sample code. Initially, only commandButton Two is visible. When I click

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.