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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:57:26+00:00 2026-05-22T15:57:26+00:00

I have these dynamic checkboxes each with ID + number +1. Could anyone tell

  • 0

I have these dynamic checkboxes each with ID + number +1. Could anyone tell me how to make this code shorter? Probably nothing difficult, but I just started with jquery and javascript, so it’s a bit complicated for me 🙁

    if(!$('#g:checked').length){
        $("#h").val("No");
} else {
        $("#h").val("Yes");
}
if(!$('#g2:checked').length){
        $("#h2").val("No");
} else {
        $("#h2").val("Yes");
}
if(!$('#g3:checked').length){
        $("#h3").val("No");
} else {
        $("#h3").val("Yes");
}
if(!$('#g4:checked').length){
        $("#h4").val("No");
} else {
        $("#h4").val("Yes");
}
if(!$('#g5:checked').length){
        $("#h5").val("No");
} else {
        $("#h5").val("Yes");
}
if(!$('#g6:checked').length){
        $("#h6").val("No");
} else {
        $("#h6").val("Yes");
}
if(!$('#g7:checked').length){
        $("#h7").val("No");
} else {
        $("#h7").val("Yes");
}
if(!$('#g8:checked').length){
        $("#h8").val("No");
} else {
        $("#h8").val("Yes");
}
if(!$('#g9:checked').length){
        $("#h9").val("No");
} else {
        $("#h9").val("Yes");
}
if(!$('#g10:checked').length){
        $("#h10").val("No");
} else {
        $("#h10").val("Yes");
}
  • 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-22T15:57:27+00:00Added an answer on May 22, 2026 at 3:57 pm

    You can use a simple loop, eg.

    var n;
    for(n=1; n<10; n++) {
        $('#h'+n).val( $('#g'+n).is(':checked') ? 'Yes' : 'No' );
    }
    

    And the tertiary operator makes the logic a little more compact as well.
    You should rename your ‘h’ and ‘g’ element ids to ‘h1’ and ‘g1’ for consistency.

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

Sidebar

Related Questions

I have this table with which I create dynamic checkboxes (the number of records
I have these 3 tables: and I'm using this code to join them together
I have dynamic page. There are multiple checkboxes & I am using this script
I have dynamic number of checkboxes in jsp page as given below. <s:iterator value=details
I have 4 PHP variables, each variable consists of dynamic checkboxes, sometimes it can
I have a problem on using LinqToSQL with paging + dynamic sorting. These are
I have these three lists with the same number of elements List<String>competitoinsIDs = new
I have these variables: boost::regex re //regular expression to use std::string stringToChange //replace this
I have X number of RadioButtons (Silverlight). Normally this value is between 2-5. Call
I have dynamic checkboxes that are generated as such <cfset counter = 0> <form

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.