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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:29:47+00:00 2026-06-03T00:29:47+00:00

I have this javascript: $(function () { $.fn.enable = function () { return this.show().removeAttr(disabled);

  • 0

I have this javascript:

$(function () {
    $.fn.enable = function () {
        return this.show().removeAttr("disabled");
    }

    $.fn.disable = function () {
        return this.hide().attr("disabled", "disabled");
    }

    var incomeType = $("#MyModel_IncomeTypeCheckBox");
    var incomeTypeStatusSection = $("#incomeTypeStatusDiv, #incomeTypeStatusDiv input, #incomeTypeStatusDiv textarea, #incomeTypeStatusDiv select");
    setControls();

    incomeType.change(function () {
        setControls();
    });

    function setControls() {
        switch (incomeType.val()) {
            case "FullTime":
                incomeTypeStatusSection.disable();
                break;
            case "PartTime":
                incomeTypeStatusSection.disable();
                break;
            case "SelfEmployed":
                incomeTypeStatusSection.enable();
                break;
            case "SocialSecurity":
                incomeTypeStatusSection.disable();
                break;
            case "Retirement":
                incomeTypeStatusSection.disable();
                break;
            case "ChildSupport":
                incomeTypeStatusSection.disable();
                break;
            case "Maintenance":
                incomeTypeStatusSection.disable();
                break;
            case "Other":
                incomeTypeStatusSection.disable();
                break;
        }
    }
});

The code in my view is simply:

<div id="incomeTypeStatusDiv">
   <!--Show some hidden inputs here-->
</div>

MyModel.IncomeTypeCheckBox is based on an enum which corresponds to the case values above.

I am able to use the above javascript with a drop down list, for example. The selection will show/hide the appropriate <div>.

I am now trying to use that same code to show/hide based on the check box list selection, but without success. The <div> I want to hide is showing up and not being hidden. What am I missing that it won’t work with a checkbox list? How do I make this work with a check box list (i.e., if I click “SelfEmployed” check box it will show appropriate <div> and when I uncheck “SelfEmployed” checkbox it will hide the <div>?

I could do:

$(document).ready(function () {
    $("input[name$='stepincomeinformation.incometypecheckbox']").click(function () {
        var check_value = $(this).val();
        if (check_value == 'selfemployed') {
            $("#incometypecheckbox_selfemployed").toggle(this.checked);
        }
    });
    $("#incometypecheckbox_selfemployed").hide();
    });

but then if the <div> is hidden, it won’t allow me to move on because it’s trying to validate. The other javascript doesn’t validate the <div> if it’s hidden.

The javascript is based upon Validating a dynamic UI with Mvc2, and has worked well for me. It’s used to disable validation when the <div> is hidden (and uses a mod to MicrosoftMvcValidation.js as well as a binder.

Any help is appreciated.

SEE FINAL RESULT BELOW (Based on Answer)

  • 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-03T00:29:48+00:00Added an answer on June 3, 2026 at 12:29 am

    in the code you posted, an incomeType variable is set:

    var incomeType = $("#MyModel_IncomeTypeCheckBox");
    

    At this point, I’m guessing that MyModel_IncomeTypeCheckBox is the name assigned to all of the incomeType checkboxes.

    If that’s the case, then this likely explains why there are issues.

    First, because MyModel_IncomeTypeCheckBox is a name, and not an ID, jQuery can’t access the value via $('#MyModel_IncomeTypeCheckBox')

    Second, even if jQuery could access what MyModel_IncomeTypeCheckBox would contain in a normal form submission, the rest of the code would only work when a single box is checked, as multiple boxes being checked would result in MyModel_IncomeTypeCheckBox containing a comma-separated string of checkbox values for the selected checkboxes.

    So, there are two options here.

    The first option is the ugly task of checking each checkbox individually by its ID, but that’s not what you want since it’s painful and inefficient.

    The second option is to change interpretation of the incomeTypes that are checked to something like this:

    function setControls() {
        $('input:checked[name="MyModel_IncomeTypeCheckBox"]:checked').each(function () {
            var checkedIncomeType = $(this).val();
            handleShowHide(checkedIncomeType);
        });
    }
    
    function handleShowHide(incomeType)
    {
        // move the switch statement here
    }
    

    Now I haven’t tested this, but it should get you most of the way there, at least. Let me know if you have any questions.

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

Sidebar

Related Questions

I have this JavaScript: var Type = function(name) { this.name = name; }; var
I have this javascript code <Script> function getGroupId(){ var group=document.getElementById(selectedOptions).value; var groupFirstLetter=group.substring(2); } </Script>
Let's say I have this javascript: <script language=javascript type=text/javascript> function addtext() { var newtext
I have this javascript: function updatePage() { if (request.readyState == 4) if (request.status ==
So I have this javascript function, it sends an ajax requests to fetch a
Hello i have this form filling javascript: function onLine(code,nn) { document.writeform.bericht.value+=code; document.writeform.bericht.focus(); document.writeform.nickname.value+=nn; write1();
As you may know, when we have this code in Javascript : function getName()
I have this javascript method: <script type=text/javascript> function MyFunction(sender, eventArgs) { if (someCondition) {
I have a JavaScript function, pop_item . I have to call this from PHP,
I have this bit of JavaScript... 15 $('.ajax_edit_address').each(function() { 16 $(this).ajaxForm({ 17 target: $(this).parents('table.address').find('tr.address_header').children(':first'),

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.