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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:01:18+00:00 2026-05-15T13:01:18+00:00

if a radio button is selected, i want a div to either show or

  • 0

if a radio button is selected, i want a div to either show or hide. here is the code:

    // add event to hide unimportant details when var is disabled and show when enabled
    $('#companyVar' + companyVarID + 'Enabled').change(function() {
        $('#companyVar' + companyVarID + 'Unimportant').fadeIn("slow");
    });
    $('#companyVar' + companyVarID + 'Disabled').change(function() {
        $('#companyVar' + companyVarID + 'Unimportant').slideUp("slow");
    });

it should work (i alert tested that the event actually runs), but i think that for some reason, the variable companyVarID is unknown inside the event function. how can i fix this?

  • 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-15T13:01:19+00:00Added an answer on May 15, 2026 at 1:01 pm

    Well, you didn’t bother giving us any context, but chances are, you’re changing the value of companyVarID after setting up these event handlers…

    Somehow, you need to preserve that value (and not just the reference to the variable, which is adequately captured by the closure).

    Nick’s solution will work, and is fairly clean, but here’s an alternate technique just to give you an idea of what’s going on…

    // ...something assigns a value to companyVarID
    
    // put event-handler wireup in an anonymous function (to be called immediately)
    // mask the outer variable with a parameter that can't be changed externally
    (function(companyVarID) 
    {
      // add event to hide unimportant details when var is disabled and show when enabled
      $('#companyVar' + companyVarID + 'Enabled').change(function() {
        $('#companyVar' + companyVarID + 'Unimportant').fadeIn("slow");
      });
      $('#companyVar' + companyVarID + 'Disabled').change(function() {
        $('#companyVar' + companyVarID + 'Unimportant').slideUp("slow");
      });
    
     // pass the value of the outer variable to our anonymous function, allowing 
     // it to be captured in the event-handler closures
    })(companyVarID);
    
    // ...something changes the value of companyVarID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to display a hidden div based on a selected radio button, for
I want to pass in the session scoped the selected radio button value. My
On selection change of radio button, I want to show a clock/timer for 5
I want to put the radio button value in the status div. The text
here's what i want to achieve: Have a grouped radio button with different values
I want to hide or show my spinner depending on what is selected in
Using jQuery I want to check if checkboxes or radio button inside a div
I'm creating a javascript method that populates lists depending on a radio button selected
How to validate the radio button is selected or not in flex 3? if
My page has about 25 radio button groups. When a radio button is selected

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.