How to make the checkbox work with the boolean variable


I have this code
<td width='15%' align = 'center'><input type="checkbox" value="<%=next_IREMCustomer.getRegistration_type()%>"></td>
registration_type is a boolean field which passes either true or false

Now what I need is that when the value is true the checkbox shows a checkmark and if it is false it doesn't show.

Any help will be greatly appreciated
Who is Participating?
jeandpConnect With a Mentor Commented:
try this:

<td width='15%' align = 'center'><input type="checkbox" value="<%=next_IREMCustomer.getRegistration_type()%>" <%=next_IREMCustomer.getRegistration_type() ? "checked" : ""%>></td>

This will add the string "checked" to the input field if registration_type is true and then the browser will put a check in the checkbox.

the html output will then look like this: <input type="checkbox" value="true" checked>
Here's the short if notation:

<td width='15%' align = 'center'><input type="checkbox" value="<%=next_IREMCustomer.getRegistration_type()?1:0%>"></td>

...uhh on second thought what you'll really wanna do is something more like this:

<td width='15%' align = 'center'><input type="checkbox" value="whatever ya wanna post to the server" <%if(next_IREMCustomer.getRegistration_type()){%>checked<%}%></td>

the value attribute has nothing to do with the checked state of the checkbox, it's simply the string that get's posted to the server and can be anything you want.  When you check to see if the checkbox is checked it's just by assuring that it's not null, so in most situations the value is not even really used.

Let me know if ya gots questions.


You could do something like this:

<td width='15%' align = 'center'><input type="checkbox" <% if (next_IREMCustomer.getRegistration_type()) out.println("CHECKED"); %>"></td>
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.