0

I have a set of Checkbox of 7 Days with common name of sessionDays. I have counted checked checkboxes using this code:

var totalDays = document.querySelectorAll('.sessionDays input[type="checkbox"]:checked').length;

Now I need to display list of selected checkboxes in HTML with Javascript. Not jQuery. I have looked into many questions but didn't found solution.

This is my HTML:

<div class="days sessionDays">
                    <label>Select Session Days</label>
                    <p class="text-muted">Select the specific days you want to come for training.</p>
                    <div class="form-check">
                      <input class="form-check-input weekend" type="checkbox" value="1" id="sessionSunday" name="sessionDays" onFocus="startCalc();" onBlur="stopCalc();" onclick="resetMorning();">
                      <label class="form-check-label" for="sessionSunday">
                        Sunday
                      </label>
                    </div>
                    <div class="form-check">
                      <input class="form-check-input weekday" type="checkbox" value="1" id="sessionMonday" name="sessionDays" onFocus="startCalc();" onBlur="stopCalc();" onclick="resetEvening();">
                      <label class="form-check-label" for="sessionMonday">
                        Monday
                      </label>
                    </div>
                    <div class="form-check">
                      <input class="form-check-input weekday" type="checkbox" value="1" id="sessionTuesday" name="sessionDays" onFocus="startCalc();" onBlur="stopCalc();" onclick="resetEvening();">
                      <label class="form-check-label" for="sessionTuesday">
                        Tuesday
                      </label>
                    </div>
                    <div class="form-check">
                      <input class="form-check-input weekday" type="checkbox" value="1" id="sessionWednesday" name="sessionDays" onFocus="startCalc();" onBlur="stopCalc();" onclick="resetEvening();">
                      <label class="form-check-label" for="sessionWednesday">
                        Wednesday
                      </label>
                    </div>
                    <div class="form-check">
                      <input class="form-check-input weekday" type="checkbox" value="1" id="sessionThursday" name="sessionDays" onFocus="startCalc();" onBlur="stopCalc();" onclick="resetEvening();">
                      <label class="form-check-label" for="sessionThursday">
                        Thursday
                      </label>
                    </div>
                    <div class="form-check">
                      <input class="form-check-input weekday" type="checkbox" value="1" id="sessionFriday" name="sessionDays" onFocus="startCalc();" onBlur="stopCalc();" onclick="resetEvening();">
                      <label class="form-check-label" for="sessionFriday">
                        Friday
                      </label>
                    </div>
                    <div class="form-check">
                      <input class="form-check-input weekend" type="checkbox" value="1" id="sessionSaturday" name="sessionDays" onFocus="startCalc();" onBlur="stopCalc();" onclick="resetMorning();">
                      <label class="form-check-label" for="sessionSaturday">
                        Saturday
                      </label>
                    </div>
                  </div>

Here is JSFiddle: http://jsfiddle.net/humanware/rjfjp2mL/

2
  • You need to provide the script you have so far, so we can run it and suggest a solution Commented May 22, 2018 at 20:53
  • Here is the jsfiddle. (full project) jsfiddle.net/humanware/rjfjp2mL Commented May 22, 2018 at 20:57

2 Answers 2

1

You are almost there. Your querySelectorAll call will return an array of the checked controls. Try this code:

<html>
<head>
    <title>Random</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>

    <div class="days sessionDays">
        <label>Select Session Days</label>
        <p class="text-muted">Select the specific days you want to come for training.</p>
        <div class="form-check">
            <input class="form-check-input weekend" type="checkbox" value="1" id="sessionSunday" name="sessionDays"   onclick="whatsChecked();">
            <label class="form-check-label" for="sessionSunday">
                Sunday
            </label>
        </div>
        <div class="form-check">
            <input class="form-check-input weekday" type="checkbox" value="1" id="sessionMonday" name="sessionDays"  onclick="whatsChecked();">
            <label class="form-check-label" for="sessionMonday">
                Monday
            </label>
        </div>
        <div class="form-check">
            <input class="form-check-input weekday" type="checkbox" value="1" id="sessionTuesday" name="sessionDays"  onclick="whatsChecked();">
            <label class="form-check-label" for="sessionTuesday">
                Tuesday
            </label>
        </div>
        <div class="form-check">
            <input class="form-check-input weekday" type="checkbox" value="1" id="sessionWednesday" name="sessionDays"  onclick="whatsChecked();">
            <label class="form-check-label" for="sessionWednesday">
                Wednesday
            </label>
        </div>
        <div class="form-check">
            <input class="form-check-input weekday" type="checkbox" value="1" id="sessionThursday" name="sessionDays"  onclick="whatsChecked();">
            <label class="form-check-label" for="sessionThursday">
                Thursday
            </label>
        </div>
        <div class="form-check">
            <input class="form-check-input weekday" type="checkbox" value="1" id="sessionFriday" name="sessionDays"  onclick="whatsChecked();">
            <label class="form-check-label" for="sessionFriday">
                Friday
            </label>
        </div>
        <div class="form-check">
            <input class="form-check-input weekend" type="checkbox" value="1" id="sessionSaturday" name="sessionDays"  onclick="whatsChecked();">
            <label class="form-check-label" for="sessionSaturday">
                Saturday
            </label>
        </div>
    </div>
    <script type="text/javascript">
        function whatsChecked() {
            var foo = [];
            var things = document.querySelectorAll('.sessionDays input[type="checkbox"]:checked');
            for (var i = 0; i < things.length; i++) {
                foo[i] = things[i].id;
            }
            for (var i = 0; i < things.length; i++) {
                console.log(things[i].id);
            }
        }

    </script>
</body>
</html>
Sign up to request clarification or add additional context in comments.

2 Comments

so you have called whatsChecked in input onclick event. What if I need to display the list of checkboxes in another div?
Simply output things[i].id to div. If you need more help please post another question.
1

var selectedDays = document.querySelectorAll('.sessionDays input[type="checkbox"]:checked'); var totalDays = selectedDays.length;

Array.from(selectedDays).(function(item, index){
// code to display every checkbox (item)
});

1 Comment

sorry. I don't understand what you are trying to say.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.