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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:34:16+00:00 2026-06-18T16:34:16+00:00

I have 2 checkboxes for yes/no. The checkboxes will hide a div based on

  • 0

I have 2 checkboxes for yes/no. The checkboxes will hide a div based on the checkbox that is checked. I have a function that works using IDs for selectors however I will have multiple of these pulled in dynamically and need this function to use classes to select closest classes to the checkbox that is clicked.

This function works using IDs but I want to use classes: http://jsfiddle.net/infatti/mNQK7/

$('#solutionImplemented1').click(function () {
    // uncheck the other checkbox and hide other content if this is checked
    if (this.checked) {
        $('#solutionImplemented2').attr('checked',false);
        $('#solutionImplementedContent2').hide(this.checked);
    }
    // show correct content
    $('#solutionImplementedContent1').toggle(this.checked);
});
$('#solutionImplemented2').click(function () {
    // uncheck the other checkbox and hide other content if this is checked
    if (this.checked) {
        $('#solutionImplemented1').attr('checked',false);
        $('#solutionImplementedContent1').hide(this.checked);
    }
    // show correct content
    $('#solutionImplementedContent2').toggle(this.checked);
});

This is not working but needs to use selectors relative to checkbox clicked: http://jsfiddle.net/infatti/n6gW5/

$('.check-hide-show input:checkbox').click(function () {

    var firstCheckbox = $(this).parent().find('input:checkbox').eq(0);
    var secondCheckbox = $(this).parent().find('input:checkbox').eq(1);
    var checkboxContent1 = $(this).parent().find().nextAll('.check-hide-show-content:gt(0)');
    var checkboxContent2 = $(this).parent().find().nextAll('.check-hide-show-content:gt(1)');

    // uncheck the other checkbox and hide other content if this is checked
    if ($(firstCheckbox).checked) {
        $(secondCheckbox).attr('checked',false);
        $(checkboxContent2).hide();
        $(checkboxContent1).show();
    }

});

How can I select elements relative to checkbox clicked? What am I not doing right here?

  • 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-18T16:34:17+00:00Added an answer on June 18, 2026 at 4:34 pm

    You’re going to find this method incredibly hard to maintain. Imagine what happens if you have 50 checkboxes; 50 if statements with 50 lines of .hide() and .show()? What I’d suggest is having each checkbox linked to its div, perhaps through a name= attribute.

    Then, you could do:

    <input type='checkbox' name='check-div1' />
    <input type='checkbox' name='check-div2' />
    <input type='checkbox' name='check-div3' />
    
    <div class='check-hide-show-content' name='div-div1'> ... </div>
    <div class='check-hide-show-content' name='div-div2'> ... </div>
    <div class='check-hide-show-content' name='div-div3'> ... </div>
    

    Then, in your code:

    $('.check-hide-show input:checkbox').click(function () {
        var divName = $(this).attr("name").substring(6); // Shave "check-" off the start
        var $targetDiv = $("div[name='div-" + divName + "']");
        $("div.check-hide-show-content").hide();
        $targetDiv.show();
    });
    

    Since jsFiddle is currently down for me, here’s an example on jsBin. Note that if you check both checkboxes, it will only use the latest checked one. You can circumvent that by adding:

    $(".check-hide-show input[type='checkbox']").not(this).prop("checked", false);
    

    (Update: Here it is on jsFiddle.)

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

Sidebar

Related Questions

I have a check box that will if checked send a gift as a
I have 3 checkboxes with the following jQuery and it works great. $(document).ready(function() {
If I have Checkboxes in page with IDs like below a_chk1, a_chk2, a_chk3, b_chk1,
I'm using the SimpleFormController and have checkboxes in the view. When the form is
For example, i have checkboxes: <input type='checkbox' name='fruit' value='apple'> Apple <input type='checkbox' name='fruit' value='orange'>
I have two checkboxes. I need to check that at least one of them
I have some checkboxes bound to an array in my model. This works great,
I have a search form where several checkboxes are checked by default. When the
I have done a program for a pop-up using Ajax ModalPopupExtender where I will
I have a simple user signup form with a checkbox that lets users get

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.