0

When a checkbox is left blank I want it to carry the value "false" and when the checkbox is checked I want it to carry the value "true".

<label for="windscreen"><b>Windscreen</b></label>
        <input class="form-control" id="windscreen" type="checkbox" name="windscreen">

        <label for="heatedchairs"><b>Heated Chairs</b></label>
        <input class="form-control" id="heatedchairs" type="checkbox" name="heatedchairs">

        <label for="indicators"><b>Indicators</b></label>
        <input class="form-control" id="indicators" type="checkbox" name="indicators">

        <input class="btn btn-primary" type="submit" id="register" name="create" value="Sign up">


<script
   function register {
   document.getElementById("windscreen").checked = true;
   document.getElementById("windscreen").checked = false;
   }>
</script>

I have tried this for one of my checkboxes that has the id="windscreen", but it didn't do anything

When the checkbox is checked it will return the value true and when it is unchecked it returns the value false. So in the database it will be saved as false or true, instead of it all being "on" as it is at the moment

  • 1
    instead of it all being "on" as it is at the moment--> please explain. Also may be you have issue with databse saving code. so add that too here – Alive to die - Anant Sep 07 '19 at 11:41
  • Have you seen https://stackoverflow.com/questions/6204885/get-checkbox-status-using-javascript ? Please fix script tag that should be ` – Manzolo Sep 07 '19 at 11:46
  • Possible duplicate of [POST unchecked HTML checkboxes](https://stackoverflow.com/questions/1809494/post-unchecked-html-checkboxes) – Casper Sep 07 '19 at 13:11

1 Answers1

0

Maybe the following code will help you to solve your problem.

<label for="windscreen"><b>Windscreen</b></label>
<input class="form-control" id="windscreen" type="checkbox" name="windscreen">

<label for="heatedchairs"><b>Heated Chairs</b></label>
<input class="form-control" id="heatedchairs" type="checkbox" name="heatedchairs">

<label for="indicators"><b>Indicators</b></label>
<input class="form-control" id="indicators" type="checkbox" name="indicators">

<input class="btn btn-primary" type="submit" id="register" name="create" value="Sign up">


<script>
    var windscreen = document.getElementById("windscreen");
    var heatedchairs = document.getElementById("heatedchairs");
    var indicators = document.getElementById("indicators");

    // default false
    windscreen.checked = false;
    heatedchairs.checked = false;
    indicators.checked = false;
    console.log('default windscreen ---- ', windscreen.checked);
    console.log('default heatedchairs ---- ', heatedchairs.checked);
    console.log('default indicators ---- ', indicators.checked);
    document.getElementById('register').onclick = function () {
        console.log('clicked windscreen ---- ', windscreen.checked);
        console.log('clicked heatedchairs ---- ', heatedchairs.checked);
        console.log('clicked indicators ---- ', indicators.checked);
    }


</script>
RamKumar
  • 126
  • 1
  • 10