How to make the checkbox work with the boolean variable

Posted on 2006-05-30
Last Modified: 2010-04-01

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
Question by:royalcyber
    LVL 12

    Expert Comment

    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.



    Expert Comment

    You could do something like this:

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

    Accepted Solution

    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>

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    After several days of searching and hunting for limited documentation, I wanted to share this guide to hopefully save someone the hassle of trying to figure this out on their own. I have tested this on Xendesktop 7.1 and PS 4.5 running simultaneous…
    I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now