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 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 a problem on using LinqToSQL with paging + dynamic sorting. These are
I have these two pieces of code, wich one is more readable? foreach decimal
I have these container objects (let's call them Container) in a list. Each of
I want to have a class with dynamic methods. From these arrays: prefix =
i have the following piece of code: <h3 style=margin: 0px; margin-bottom: 20px;>Click the checkboxes
I have X number of RadioButtons (Silverlight). Normally this value is between 2-5. Call
I've found various examples in jsf where folks have done this successfully; in these
I have these vertically positioned divs, having dynamic content in them, but how to

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.