Interview Questions

Java Script Select All CheckBox in a JSP

Consider a web page where you display a list of values in a tabular format. Assume that each row in that table has a check box either as a first or last element. You may be faced with a requirement to select/deselect all the check boxes. Use the following JSP code which uses java scrip to select/deselect check boxes.

<%@page import="java.util.List" %>

<%@page import="java.util.ArrayList" %>  

<%@ taglib uri=""  prefix="c" %>

<title>CheckBox Example</title>
<h1>CheckBox Example</h1>
List langLst = new ArrayList();
langLst.add("Small Talk");

<input type=checkbox name='selectAllCheck' onClick='javascript:funcSelectAll()' value='Select All'>Select All </input>
<TABLE border="1">
           <B>Programming Language List </B>

<c:forEach var="item" items="${requestScope.langLst}">
           <input type=checkbox name='lang' ><c:out value="${item}" /> </input>

<script language="javascript">
function funcSelectAll()
            for (var a=0; a < document.forms[0].lang.length; a++)        {
                 document.forms[0].lang[a].checked = true;            
           for (var a=0; a < document.forms[0].lang.length; a++)        {
                  document.forms[0].lang[a].checked = false;          


The above JSP code uses scriptlet code only for example purpose. In real world applications use either JSP standard tags or Expression language.