Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML - checkbox conditionally checked

I have written this simple jsp page: I want to show a simple list of object (Person), with a label (showing the name of the person) and a checkbox (that shows if the person is at least 18 yo).

I also write the corresponding Java class 'Person', with a String (name) and a boolean (isAdult).

Here is my jsp (personList is a List of Person objects)

 <table>
     <th>NAME</th>
     <th>IS ADULT</th>
        <c:forEach var="person" items="${personList}">
           <tr>
             <td>${person.name}</td>
             <td>
               <input type="checkbox" value="checked"/>
             </td>
           </tr>
        </c:forEach>
 </table>

This portion of code shows me a list in which all checkboxes are not checked (names are correct.

How could I obtain that every checkbox in the list is checked (or not), referring to the boolean attribute 'isAdult' of the Person object?

like image 874
Adriano Bellavita Avatar asked Feb 06 '26 00:02

Adriano Bellavita


1 Answers

You can use EL ternary operator like this:

 <input type="checkbox"  ${person.adult ? 'checked' : ''}>

${person.adult} will invoke isAdult method that encapsulating adult attribute.

like image 64
Gurkan Yesilyurt Avatar answered Feb 08 '26 12:02

Gurkan Yesilyurt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!