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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:52:50+00:00 2026-05-22T01:52:50+00:00

I wrote a script that shows/hides a div depending on which checkbox is selected.

  • 0

I wrote a script that shows/hides a div depending on which checkbox is selected. The code I wrote below works fine, but…

Is there a way to shorten this code, without having to repeat the if/else statement for each additional checkbox/div I will need to add?

$(function() {
    $("#submit_button").click(function(){
        if ($("input[value='poster1']:checked").val()){
            $("#results1").show();
        }
        else {
            $("#results1").hide();
        }
        if ($("input[value='poster2']:checked").val()){
            $("#results2").show();
        }
        else {
            $("#results2").hide();
        }
        if ($("input[value='poster3']:checked").val()){
            $("#results3").show();
        }
        else {
            $("#results3").hide();
        }

});
  • 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-22T01:52:50+00:00Added an answer on May 22, 2026 at 1:52 am
    $("#submit_button").click(function() {
        $('input').each(function(i,el){
            var $el = $(el),
                num = $el.val().match(/poster(\d*)/)[1];
            if ($el.filter(':checked').length) {
                $('#results'+num).show();
            } else {
                $('#results'+num).hide();
            }
        });
    });
    

    Demo →

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

Sidebar

Related Questions

I'm trying to write a script that will hidden/show div depending on other elements
I wrote a script that uses xcodebuild to generate an AdHoc build of an
I wrote a Python script that does some task to generate, and then keep
This is a crude korn shell script that someone else wrote. I don't know
I have a Perl script I wrote for my own personal use that fetches
I wrote a short bash script to complete a task that involves creating a
I have a Python script I recently wrote that I call using the command
I am trying to write an apple script that sequentially names my TV shows
I wrote a short script that never terminates. This script continuously generates output that
I wrote a script in which it retrieves data from a database and i

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.