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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:38:04+00:00 2026-06-12T15:38:04+00:00

I am looping through a list of Java Strings using JSP and generating HTML

  • 0

I am looping through a list of Java Strings using JSP and generating HTML checkboxes for each String like so:

<%
while (st.hasMoreTokens())
{
    String object = st.nextToken();

    String temp = "<li><input type=\"checkbox\" id=\"" + object +
     "\" + name=\"type\" value =\"" + object + "\">" + object + "</li>";

    out.println(temp);
}
%>

However, because there are around 100 checkboxes, it would be time-consuming selecting ~40 options that the user wants to select. Instead, I’d like to create a drop-down menu that has custom hard-coded lists that would select the appropriate checkboxes.

For example, if I had a list of checkboxes as follows:

Apple
Orange
Banana
Pepper
Lime
Lemon
Peas

I could create custom lists like the following:

var fruit={Apple, Orange, Banana, Lime, Lemon}
var veg="{Pepper, Peas}

Then, I could create a drop-down with “Fruits” and “Vegetables” as the only two options. If the user selects “Fruits,” then JavaScript would check “Apple”, “Orange”, “Banana”, “Lime”, and “Lemon”, and vice versa.

It’s important that the user should still be able to select one or a few checkboxes manually, so the huge list of checkboxes is essential.

  • 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-12T15:38:05+00:00Added an answer on June 12, 2026 at 3:38 pm

    The idea is to give your checkboxes matching names and grab an array of all the elements that match the name in the dropdown using document.getElementsByName()

    HTML

    <select id="type" onChange="selectType();">
        <option value=""></option>
        <option value="fruit">Fruit</option>
        <option value="vegs">Vegs</option>
    </select>
    
    <input type="checkbox" name="fruit" id="Apple">Apple</input>
    <input type="checkbox" name="fruit" id="Orange">Orange</input>
    <input type="checkbox" name="vegs" id="Carrot">Carrot</input>
    <input type="checkbox" name="vegs" id="Peas">Peas</input>​
    

    JAVASCRIPT

    function selectType(){
        var el = document.getElementById("type");
    
        if(el.value !== ""){
          var type = document.getElementsByName(el.value);
          for(var i = 0; i < type.length; i++){
            type[i].checked = true;
          }
        }    
    }
    

    EXAMPLE

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

Sidebar

Related Questions

I am looping through a list for a spellchecker in vb.net (using vs 2010).
I'm looping through a List of elements with foreach, like this: foreach (Type name
I'm trying to iterate throuh a list while already looping through it (nested loops).
I am looping through a list of samaccountnames and performing several actions: # Disabling
I've been looping through the contents of a list in the simple fashion to
Looping through each id retrieve value of objects in 1 dimentional array and 2
While looping through a Dataset which code snippet should I use, should I go
I'm looping through an associative array with a foreach. I'd like to be able
I'm very new to Java and am having some issues looping through JCheckBoxes on
Looking through the implementation of the Java HashMap here : http://www.docjar.com/html/api/java/util/HashMap.java.html I noticed the

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.