2

Hi I'm currently using radio buttons to pass True or False as a string in my project. I would like to change it to a checkbox that returns the same values, i.e "True" or "False" (based on whether the checkbox is checked or unchecked) so that I'm not forced to change the condition everywhere else in my project.

<input type="radio" id="admin"
        name="access" value="True">Access<br>
        <input type="radio" id="donotaccess"
        name="access" value="False" checked="">Do not access<br></input>  

Any suggestions for the same?

Code for radio button added

2 Answers2

0

You can store the value of the checkbox inside a variable hasAccess

In the following code - hasAccess is equal to True or False (string)

When you need to verify that the checkbox is checked or not you should use the variable hasAccess

var hasAccess;

function verifyCheckbox(elem) {
  hasAccess = elem.checked.toString();
  hasAccess = hasAccess.charAt(0).toUpperCase() + hasAccess.slice(1);
  console.log(hasAccess);
}
<input type="checkbox" id="gquotas-admin-xptable-access" name="xp_table_access" onclick="verifyCheckbox(this)">Access

NOTE

You should think about using boolean instead of True/False strings

Weedoze
  • 13,683
  • 1
  • 33
  • 63
0

Here's a simple way of doing it:

$('#isSelected').change(function(){
    var checkboxValue = $(this).is(':checked');
    document.getElementById('returned').innerHTML=checkboxValue;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="isSelected"/>
    <div id = "returned">
    Value Displayed Here
    </div>

This is done in jQuery, is(':checked') returns true if checkbox is selected, and false if it is not.

Hope this helps.

almost a beginner
  • 1,622
  • 2
  • 20
  • 41