How to select all checkbox when i checked all check boxes - javascript

I want to select all checkbox checked when i select all checkboxes in my dropdown menu. I am a beginner js developer need help. This is my js code
var expanded = false;
function showCheckboxes() {
var checkboxes = document.getElementById("checkboxes");
if (!expanded) {
checkboxes.style.display = "block";
expanded = true;
} else {
checkboxes.style.display = "none";
expanded = false;
}
}
var selectallBox = document.getElementById('selectall');
var checkBoxes = document.querySelectorAll('.select-me');
selectallBox.addEventListener('click', function() {
for (var i = 0; i < checkBoxes.length; i++) {
if (checkBoxes[i] != selectallBox)
checkBoxes[i].checked = selectallBox.checked;
}
})
for (var i = 0; i < checkBoxes.length; i++) {
checkBoxes[i].addEventListener('click', function() {
selectallBox.checked = false;
})
}
document.querySelector('.close-check-box').addEventListener('click' , function(){
checkboxes.style.display = 'none'
})
This is my js fiddle:
https://jsfiddle.net/b9ueL3fw/

You can compare the total check box with the checked check box count, if both length are same then set check box checked property to true, otherwise set to false:
var selectedCount = document.querySelectorAll('.select-me:checked').length;
if(checkBoxes.length == selectedCount){
selectallBox.checked = true;
}
else{
selectallBox.checked = false;
}
var expanded = false;
var selectBox = document.querySelector('.selectBox')
selectBox.addEventListener('click', function(){
var checkboxes = document.getElementById("checkboxes");
if (!expanded) {
checkboxes.style.display = "block";
expanded = true;
} else {
checkboxes.style.display = "none";
expanded = false;
}
});
var selectallBox = document.getElementById('selectall');
var checkBoxes = document.querySelectorAll('.select-me');
selectallBox.addEventListener('click', function() {
for (let i = 0; i < checkBoxes.length; i++) {
if (checkBoxes[i] != selectallBox)
checkBoxes[i].checked = selectallBox.checked;
}
})
for (var i = 0; i < checkBoxes.length; i++) {
checkBoxes[i].addEventListener('click', function() {
var selectedCount = document.querySelectorAll('.select-me:checked').length;
if(checkBoxes.length == selectedCount){
selectallBox.checked = true;
}
else{
selectallBox.checked = false;
}
});
}
document.querySelector('.close-check-box').addEventListener('click' , function(){
checkboxes.style.display = 'none'
})
.multiselect {
width: 400px;
}
.selectBox {
position: relative;
}
.selectBox select {
width: 100%;
}
.overSelect {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
}
#checkboxes {
display: none;
border: 1px rgb(169, 169, 169) solid;
padding-top: 10px;
border-radius: 5px;
margin-top: 10px;
height: 200px;
overflow-y: scroll;
}
label {
display: block;
}
select {
padding: 6px 12px;
border-radius: 5px;
}
.close-check-box:link,
.close-check-box:visited {
padding: 1px 25px;
text-decoration: none;
font-weight: 300;
background-color: #3498db;
border: 1px solid #3498db;
color: #fff;
margin-left: 14px;
box-shadow: 0 2px 5px 0 rgba(0,0,0,0.16), 0 2px 10px 0 rgba(0,0,0,0.12);
margin-top: 10px;
margin-bottom: 20px;
display: inline-block;
}
hr.style-two {
border: 0;
height: 1px;
background-image: linear-gradient(to right, rgba(169, 169, 169, 0), rgba(169, 169, 169, 0.75), rgba(169, 169, 169, 0));
}
<form method="GET">
<div class="multiselect">
<div class="selectBox">
<select>
<option>Select an option</option>
</select>
<div class="overSelect"></div>
</div>
<div id="checkboxes">
<label for="selectall">
<input type="checkbox" id="selectall" / class="mr-3 ml-3">Select All</label>
<hr class="style-two">
<label for="one">
<input type="checkbox" name="checkbox1" value="1" id="one" / class="mr-3 ml-3 select-me">First</label>
<label for="two">
<input type="checkbox" name="checkbox2" value="2" id="two" / class="mr-3 ml-3 select-me">Second</label>
<label for="three">
<input type="checkbox" name="checkbox3" value="3" id="three" / class="mr-3 ml-3 select-me">First</label>
<label for="four">
<input type="checkbox" name="checkbox4" value="4" id="four" / class="mr-3 ml-3 select-me">Second</label>
<label for="five">
<input type="checkbox" name="checkbox5" value="5" id="five" / class="mr-3 ml-3 select-me">First</label>
<label for="six">
<input type="checkbox" name="checkbox6" value="6" id="six" / class="mr-3 ml-3 select-me">Second</label>
<label for="seven">
<input type="checkbox" name="checkbox7" value="7" id="seven" / class="mr-3 ml-3 select-me">First</label>
<label for="eight">
<input type="checkbox" name="checkbox8" value="8" id="eight" / class="mr-3 ml-3 select-me">Second</label>
<label for="nine">
<input type="checkbox" name="checkbox9" value="9" id="nine" / class="mr-3 ml-3 select-me">First</label>
<label for="ten">
<input type="checkbox" name="checkbox10" value="10" id="ten" / class="mr-3 ml-3 select-me">Second</label>
<a class="close-check-box" href="#">click</a>
</div>
</div>
<button type="submit">submit</button>
</form>

var expanded = false;
var selectBox = document.querySelector(".selectBox");
selectBox.addEventListener("click", function() {
var checkboxes = document.getElementById("checkboxes");
if (!expanded) {
checkboxes.style.display = "block";
expanded = true;
} else {
checkboxes.style.display = "none";
expanded = false;
}
});
let checkboxesCheckedCount = 0;
var selectallBox = document.getElementById("selectall");
var checkBoxes = document.querySelectorAll(".select-me");
selectallBox.addEventListener("click", function() {
for (var i = 0; i < checkBoxes.length; i++) {
if (checkBoxes[i] != selectallBox)
checkBoxes[i].checked = selectallBox.checked;
checkboxesCheckedCount = selectallBox.checked ? checkBoxes.length : 0;
}
});
for (var i = 0; i < checkBoxes.length; i++) {
checkBoxes[i].addEventListener("click", function() {
selectallBox.checked = false;
checkboxesCheckedCount = this.checked ? checkboxesCheckedCount +1 : checkboxesCheckedCount -1
if (checkboxesCheckedCount === checkBoxes.length) {
selectallBox.checked = true;
}
});
}
document
.querySelector(".close-check-box")
.addEventListener("click", function() {
checkboxes.style.display = "none";
});
.multiselect{width:400px}.selectBox{position:relative}.selectBox select{width:100%}.overSelect{position:absolute;left:0;right:0;top:0;bottom:0}#checkboxes{display:none;border:1px #a9a9a9 solid;padding-top:10px;border-radius:5px;margin-top:10px;height:200px;overflow-y:scroll}label{display:block}select{padding:6px 12px;border-radius:5px}.close-check-box:link,.close-check-box:visited{padding:1px 25px;text-decoration:none;font-weight:300;background-color:#3498db;border:1px solid #3498db;color:#fff;margin-left:14px;box-shadow:0 2px 5px 0 rgba(0,0,0,.16),0 2px 10px 0 rgba(0,0,0,.12);margin-top:10px;margin-bottom:20px;display:inline-block}hr.style-two{border:0;height:1px;background-image:linear-gradient(to right,rgba(169,169,169,0),rgba(169,169,169,.75),rgba(169,169,169,0))}
<form method="GET"><div class="multiselect"><div class="selectBox"> <select><option>Select an option</option></select><div class="overSelect"></div></div><div id="checkboxes"><label for="selectall"> <input type="checkbox" id="selectall" / class="mr-3 ml-3">Select All</label><hr class="style-two"> <label for="one"> <input type="checkbox" name="checkbox1" value="1" id="one" / class="mr-3 ml-3 select-me">First</label> <label for="two"> <input type="checkbox" name="checkbox2" value="2" id="two" / class="mr-3 ml-3 select-me">Second</label> <label for="three"> <input type="checkbox" name="checkbox3" value="3" id="three" / class="mr-3 ml-3 select-me">First</label> <label for="four"> <input type="checkbox" name="checkbox4" value="4" id="four" / class="mr-3 ml-3 select-me">Second</label> <label for="five"> <input type="checkbox" name="checkbox5" value="5" id="five" / class="mr-3 ml-3 select-me">First</label> <label for="six"> <input type="checkbox" name="checkbox6" value="6" id="six" / class="mr-3 ml-3 select-me">Second</label> <label for="seven"> <input type="checkbox" name="checkbox7" value="7" id="seven" / class="mr-3 ml-3 select-me">First</label> <label for="eight"> <input type="checkbox" name="checkbox8" value="8" id="eight" / class="mr-3 ml-3 select-me">Second</label> <label for="nine"> <input type="checkbox" name="checkbox9" value="9" id="nine" / class="mr-3 ml-3 select-me">First</label> <label for="ten"> <input type="checkbox" name="checkbox10" value="10" id="ten" / class="mr-3 ml-3 select-me">Second</label><a class="close-check-box" href="#">click</a></div></div> <button type="submit">submit</button></form>

Related

body or window outside of element remove same element with click in javascript

want to delete when the user clicks anywhere on the page except the .
In addition, the checked to remain in after remove or when the user clicks No, remove checked in and
Please help me to do this work. If you have a suggestion for solving this problem, thank you for letting me know. Thank you in advance for your cooperation.
const confirm = document.getElementById('confirm');
confirm.addEventListener('click', function() {
const yes = document.querySelector('input[type="checkbox"]:checked');
const yesItems = document.getElementById('yes-items');
if (yes.checked == true) {
yesItems.style.display = "block";
}
document.querySelector('body').addEventListener('click', function(e) {
if (e.target.yes.checked == true) {
yesItems.style.display = "none";
}
});
});
items {
border: 1px solid red;
padding: .5rem;
border-radius: 10px;
}
span.yes-items {
position: absolute;
border: 2px solid var(--c1);
color: var(--txt);
background-color: #e6eef7;
top: 20%;
left: 0;
width: 94%;
padding: 1rem;
border-radius: 10px;
display: none;
}
span.yes-items input {
width: auto;
}
<p class="items">
<span class="label">example</span>
<span class="label-items">
<label class="label-item" for="okey" id="confirm">
<input type="checkbox" name="Yes" id="okey">
Yes
</label>
<label class="label-item">
<input type="checkbox" name="No" id="No">
No
</label>
</span>
<span class="yes-items" id="yes-items"><span>
<label for="Diabetic">
<input type="checkbox" name="Diabetic" id="Diabetic">
1
</label>
</span>
<span>
<label for="Diabetic">
<input type="checkbox" name="Diabetic" id="Diabetic">
2
</label>
</span>
<span>
<label for="Diabetic">
<input type="checkbox" name="Diabetic" id="Diabetic">
3
</label>
</span>
<span>
<label for="Diabetic">
<input type="checkbox" name="Diabetic" id="Diabetic">
4
</label>
</span>
<span>
<label for="Diabetic">
<input type="checkbox" name="Diabetic" id="Diabetic">
other
<textarea class="explain" placeholder="Define Other" cols="50" rows="3"></textarea>
</label>
</span>
</span>
</p>
Its Very simple,
Just Create And Put All Items into div#body
And You Have to use the event window.onclick
Then create if statement and check event.target.id == "body" ( To Check The Clicked Element is the div#body ( Not Its Children ) )
if (event.target.id == "body"){ /* Hiding Code */ }
Example
var w = window;
var a = document.getElementById("example");
var c = document.getElementById("okay");
var e = document.getElementById("yes-items");
a.addEventListener("click", check);
w.addEventListener("click", clickedBody);
function clickedBody(event){
if(event.target.id == "body"){
hideExplainIfOpened();
}
}
function check(){
if(c.checked){ showExplain();}
else{ hideExplain(); }
}
function hideExplainIfOpened(){
if(!e.classList.contains("d-none")){
hideExplain();
}
}
function showExplain(){ e.classList.remove("d-none"); }
function hideExplain(){ e.classList.add("d-none"); }
#body {
position:absolute;
top:0;
bottom:0;
left:0;
right:0;
overflow:auto;
}
items {
border: 1px solid red;
padding: .5rem;
border-radius: 10px;
}
span.yes-items {
position: absolute;
border: 2px solid var(--c1);
color: var(--txt);
background-color: #e6eef7;
top: 20%;
left: 0;
width: 94%;
padding: 1rem;
border-radius: 10px;
}
.d-none{ display:none; }
span.yes-items input {
width: auto;
}
<div id="body">
<p class="items">
<span class="label">example</span>
<span class="label-items" id="example">
<label class="label-item" for="okey" id="confirm">
<input type="radio" class="input" name="example" value="1" id="okay">
Yes
</label>
<label class="label-item">
<input type="radio" class="input" name="example" value="0" id="No">
No
</label>
</span>
<span class="yes-items d-none" id="yes-items"><span>
<label for="Diabetic">
<input type="checkbox" name="Diabetic" id="Diabetic">
1
</label>
</span>
<span>
<label for="Diabetic">
<input type="checkbox" name="Diabetic" id="Diabetic">
2
</label>
</span>
<span>
<label for="Diabetic">
<input type="checkbox" name="Diabetic" id="Diabetic">
3
</label>
</span>
<span>
<label for="Diabetic">
<input type="checkbox" name="Diabetic" id="Diabetic">
4
</label>
</span>
<span>
<label for="Diabetic">
<input type="checkbox" name="Diabetic" id="Diabetic">
other
<textarea class="explain" placeholder="Define Other" cols="50" rows="3"></textarea>
</label>
</span>
</span>
</p>
</div>

Showing a Result Table based on an array of Radio Buttons selected

I am new to coding. I'm trying to build an online form where there are a few questions that user will need to answer and questions are done in radio button options. Based on the array of radio buttons selected, I will then need to show the results on the same page. I'm thinking of using if..else statement but it doesn't seem to be working.
Here is my code:
<script>
function ShowHideDiv() {
if (document.getElementById("chkCitizen").checked ? && document.getElementById("chkMale").checked ?
&& document.getElementById("chkSingle").checked ? && document.getElementById("chkEmployed").checked ?
&& document.getElementById("chkNo").checked ? )
{
btn1.style.visibility = "visible";
var dvtext = document.getElementById("dvtext");
dvtext.style.display = "block" : "none";
}
else if (document.getElementById("chkCitizen") && document.getElementById("chkFemale")
&& document.getElementById("chkSingle") && document.getElementById("chkEmployed")
&& document.getElementById("chkNo")
{
btn1.style.visibility = "visible";
var dvtext = document.getElementById("dvtext");
dvtext.style.display = "block" : "none";
}
}
function resetFunction(){
document.getElementById("reliefChecker").reset();
}
</script>
Here's the full code with html and css:
<script>
function ShowHideDiv() {
if (document.getElementById("chkCitizen").checked && document.getElementById("chkMale").checked
&& document.getElementById("chkSingle").checked && document.getElementById("chkEmployed").checked
&& document.getElementById("chkNo").checked)
{
btn1.style.visibility = "visible";
var btn1 = document.getElementById("btn1");
var dvtext = document.getElementById("dvtext");
dvtext.style.display = "block" : "none";
}
else if (document.getElementById("chkCitizen").checked && document.getElementById("chkFemale").checked
&& document.getElementById("chkSingle").checked && document.getElementById("chkEmployed").checked
&& document.getElementById("chkNo").checked)
{
btn1.style.visibility = "visible";
var btn1 = document.getElementById("btn1");
var dvtext = document.getElementById("dvtext");
dvtext.style.display = "block" : "none";
}
else if (document.getElementById("chkPR").checked && document.getElementById("chkMale").checked
&& document.getElementById("chkSingle").checked && document.getElementById("chkEmployed").checked
&& document.getElementById("chkNo").checked)
{
btn1.style.visibility = "visible";
var btn1 = document.getElementById("btn1");
var dvtext = document.getElementById("dvtext");
dvtext.style.display = "block" : "none";
}
else if (document.getElementById("chkPR").checked && document.getElementById("chkFemale").checked
&& document.getElementById("chkSingle").checked && document.getElementById("chkEmployed").checked
&& document.getElementById("chkNo").checked)
{
btn1.style.visibility = "visible";
var btn1 = document.getElementById("btn1");
var dvtext = document.getElementById("dvtext");
dvtext.style.display = "block" : "none";
}
else if (document.getElementById("chkForeigner").checked && document.getElementById("chkMale").checked
&& document.getElementById("chkSingle").checked && document.getElementById("chkEmployed").checked
&& document.getElementById("chkNo").checked)
{
btn1.style.visibility = "visible";
var btn1 = document.getElementById("btn1");
var dvtext = document.getElementById("dvtext");
dvtext.style.display = "block" : "none";
}
else (document.getElementById("chkForeigner").checked && document.getElementById("chkFemale").checked
&& document.getElementById("chkSingle").checked && document.getElementById("chkEmployed").checked
&& document.getElementById("chkNo").checked)
{
btn1.style.visibility = "visible";
var btn1 = document.getElementById("btn1");
var dvtext = document.getElementById("dvtext");
dvtext.style.display = "block" : "none";
}
}
function resetFunction(){
document.getElementById("reliefChecker").reset();
}
</script>
.button {
background-color: #F7922C;
border-radius: 12px;
border: 2px solid white;
color: white;
padding: 10px 40px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 20px;
margin: none;
cursor: pointer;
visibility: block;
}
.buttonReset {
background-color: #FFFFFF;
border-radius: 12px;
border: 2px solid #005AAD;
color: black;
padding: 10px 40px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 20px;
margin: none;
cursor: pointer;
visibility: hidden;
}
.center {
text-align:center;
}
<h1><p style="font-family:Arial">Checker</p></h1>
<form id="reliefChecker">
<fieldset id="reliefChecker"; style="padding:10px;border:0px solid #FFFFFF;">
<!-- Second Question -->
<p style="font-family:Arial; font-size:25px;font-weight:bold">
Gender <span style="color:red">*</span></p>
<label for="chkMale" style="font-family:Arial; font-size:20px;">
<input type="radio" id="chkMale" name="gender" value="male" onclick="ShowHideDiv()">
Male</label>
<br>
<label for="chkFemale" style="font-family:Arial; font-size:20px;">
<input type="radio" id="chkFemale" name="gender" value="female" onclick="ShowHideDiv()">
Female</label>
<!-- Third Question -->
<p style="font-family:Arial; font-size:25px;font-weight:bold">
Marital Status <span style="color:red">*</span></p>
<label for="chkMarried" style="font-family:Arial; font-size:20px;">
<input type="radio" id="chkMarried" name="maritalStatus" value="married" onclick="ShowHideDiv()">
Married</label>
<br>
<label for="chkSingle" style="font-family:Arial; font-size:20px;">
<input type="radio" id="chkSingle" name="maritalStatus" value="single" onclick="ShowHideDiv()">
Single</label>
<br>
<label for="chkDivorced" style="font-family:Arial; font-size:20px;">
<input type="radio" id="chkDivorced" name="maritalStatus" value="divorce" onclick="ShowHideDiv()">
Divorced</label>
<!-- Fourth Question -->
<p style="font-family:Arial; font-size:25px;font-weight:bold">
Employment Status <span style="color:red">*</span></p>
<label for="chkEmployed" style="font-family:Arial; font-size:20px;">
<input type="radio" id="chkEmployed" name="EmploymentStatus" value="employed" onclick="ShowHideDiv()">
Employee / Self-Employed (Including Part-Timers)</label>
<br>
<label for="chkUnemployed" style="font-family:Arial; font-size:20px;">
<input type="radio" id="chkUnemployed" name="EmploymentStatus" value="unemployed" onclick="ShowHideDiv()">
Unemployed</label>
<!-- Fifth Question -->
<p style="font-family:Arial; font-size:25px;font-weight:bold">
Do you have any children? <span style="color:red">*</span></p>
<label for="chkYes" style="font-family:Arial; font-size:20px;">
<input type="radio" id="chkYes" name="Children" value="yes">
Yes</label>
<br>
<label for="chkNo" style="font-family:Arial; font-size:20px;">
<input type="radio" id="chkNo" name="Children" value="no" onclick="ShowHideDiv()">
No</label>
<br><br>
<div class="center">
<input type="button" id="btn1" class="button center" value="Check Now" onclick="ShowHideDiv()">
</div>
<div id="dvtext" style="padding:20px;border:1px solid lightgray; border-radius:15px; margin:15px; background-color:#FFFAC7; font-size:22px; font-family:Arial;display:none">
<span style="font-size:24px"><b><u>Results</u></b></span>
<br><br>
Show some results here!
<div class="center">
<input type="button" id="btnreset1" class="buttonReset center" value="Reset" onclick="ShowHideDiv(); resetFunction()">
</div>
</fieldset>
</form>
</body>
</html>
You can try this one (as I understood, it works fine (if you have more questions, it can be done with loops) ).
HTML code is:
<div class="questions">
<div class="question"> Question 1
<label>
<input class="answer" type="radio" name="q1" value="one">One
</label>
<label>
<input class="answer" type="radio" name="q1" value="two">Two
</label>
<label>
<input class="answer" type="radio" name="q1" value="three">Three
</label>
<label>
<input class="answer" type="radio" name="q1" value="four">Four
</label>
</div>
<br>
<div class="question"> Question 2
<label>
<input class="answer" type="radio" name="q2" value="one">One
</label>
<label>
<input class="answer" type="radio" name="q2" value="two">Two
</label>
<label>
<input class="answer" type="radio" name="q2" value="three">Three
</label>
<label>
<input class="answer" type="radio" name="q2" value="four">Four
</label>
</div>
</div>
<br>
<button id="submit">Submit</button>
CSS code is:
.question {
display: flex;
flex-direction: column;
}
JS code is:
var submit = document.getElementById('submit');
submit.addEventListener('click', function() {
var answers = document.getElementsByClassName('answer');
var result = "";
for(var i=0; i<answers.length; i++) {
if (answers[i].checked) {
result += answers[i].value + "\n";
}
}
alert(result);
});
HTML
<div class="questions">
<div class="question"> Question 1
<label>
<input class="answer" type="radio" name="q1" value="one">One
</label>
<label>
<input class="answer" type="radio" name="q1" value="two">Two
</label>
<label>
<input class="answer" type="radio" name="q1" value="three">Three
</label>
<label>
<input class="answer" type="radio" name="q1" value="four">Four
</label>
</div>
<br>
<div class="question"> Question 2
<label>
<input class="answer" type="radio" name="q2" value="one">One
</label>
<label>
<input class="answer" type="radio" name="q2" value="two">Two
</label>
<label>
<input class="answer" type="radio" name="q2" value="three">Three
</label>
<label>
<input class="answer" type="radio" name="q2" value="four">Four
</label>
</div>
<br>
<button id="submit">Submit</button>
</div>
CSS
.question {
display: flex;
flex-direction: column;
}
JS
var submit = document.getElementById('submit');
var result = document.getElementById('result');
var resultText = document.getElementById('result-text');
var questions = document.getElementsByClassName('questions')[0];
submit.addEventListener('click', function() {
var answers = document.getElementsByClassName('answer');
var r = "";
for(var i=0; i<answers.length; i++) {
if (answers[i].checked) {
r += answers[i].value + "\n";
}
}
questions.style.display = 'none';
result.style.height = '300px';
resultText.innerHTML = r;
});

Why my functionality doesn't work after conditionally innerHTML?

I have a few checkboxes which represent subjects. And I have two custom(radio) select. I want to change innerHTML of my first select due to checkbox:checked length. If selected chekboxes length less than 3 I want to render different select options else another select options. When page load it works fine but after recheck my checkboxes I can not select any option from my first chekbox. Can someone please look at and help me
codepen link is also here
function subjectChange (){
var subjectName = document.getElementsByName('subject-name');
var questionSel = document.getElementsByClassName('options-container')[0];
var checkBoxlength = 0;
for(var i = 0; i < subjectName.length; i++) {
if(subjectName[i].checked){
checkBoxlength++
}
}
if(checkBoxlength <= 3) {
questionSel.innerHTML = `
<label class="option">
<input type="radio" class="radio first" value="5" name="question" checked>
<span>5</span>
</label>
<label class="option">
<input type="radio" class="radio first" value="10" name="question" checked>
<span>10</span>
</label>
`;
} else if(checkBoxlength >= 4) {
questionSel.innerHTML = `
<label class="option">
<input type="radio" class="radio first" value="5" name="question" checked>
<span>5</span>
</label>
<label class="option">
<input type="radio" class="radio first" value="10" name="question" checked>
<span>10</span>
</label>
<label class="option">
<input type="radio" class="radio first" value="15" name="question" checked>
<span>15</span>
</label>
`;
}
}
const selectBox = document.querySelectorAll('.select-box')
for(var i = 0; i < selectBox.length; i++) {
const selected = selectBox[i].querySelector('.selected');
const optionsContainer = selectBox[i].querySelector('.options-container');
const optionList = selectBox[i].querySelectorAll('.option');
selected.addEventListener('click', function(){
optionsContainer.classList.toggle("active");
})
optionList.forEach( o => {
var optionListItem= o.querySelectorAll('input[type="radio"]:checked + span');
optionListItem.forEach(e => selected.innerHTML = e.innerHTML)
o.addEventListener('click', function(){
var optionListItem= o.querySelectorAll('input[type="radio"]:checked + span');
optionListItem.forEach(e => selected.innerHTML = e.innerHTML)
optionsContainer.classList.remove('active')
var firstOptionList = document.querySelector('input[type="radio"]:checked');
var secondOptionList = document.querySelectorAll('.radio.second');
secondOptionList.forEach(e => {
e.removeAttribute('checked')
if(e.value == (firstOptionList.value * 2)){
//e.checked = true;
e.setAttribute('checked','');
var optionListItem2= document.querySelectorAll('input[type="radio"]:checked + span');
const selectedTwo = document.getElementsByClassName('selected');
optionListItem2.forEach(e => selectedTwo[1].innerHTML = e.innerHTML);
console.log(e)
}
})
})
})
}
.select-box {
display: flex;
flex-direction: column;
width: 400px;
}
.select-box .options-container {
background: cornflowerblue;
color: white;
max-height: 0;
width: 100%;
opacity: 0;
transition: all .3s;
border-radius: 8px;
overflow: hidden;
order: 1;
}
.selected {
background: cornflowerblue;
color: white;
border-radius: 8px;
margin-bottom: 8px;
position: relative;
order: 0;
}
.selected::after {
content: '';
background: url('./arrow-down.svg');
background-size: contain;
background-repeat: no-repeat;
position: absolute;
height: 100%;
width: 18px;
right: 10px;
top: 40%;
transform: all .3s;
}
.select-box .option {
display: flex;
}
.select-box .option,
.selected {
padding: 12px 24px;
cursor: pointer;
}
.select-box span {
cursor: pointer;
}
.select-box .option:hover {
background: crimson;
}
.select-box .option .radio {
display: none;
}
.select-box .options-container.active {
max-height: 200px;
opacity: 1;
overflow-y: scroll;
}
.select-box .options-container.active + .selected::after {
transform: rotateX(-180deg);
top: -40%;
}
.select-box .options-container::-webkit-scrollbar {
width: 8px;
background: green;
border-radius: 0 8px 8px 0;
}
.select-box .options-container::-webkit-scrollbar-thumb {
background: red;
border-radius: 0 8px 8px 0;
}
<div class="subject-container">
<div>
<input name="subject-name" onchange="subjectChange()" type="checkbox" id="checkbox1" checked />
<label for="checkbox1">USA</label>
</div>
<div>
<input name="subject-name" onchange="subjectChange()" type="checkbox" id="checkbox2" checked />
<label for="checkbox2">Canada</label>
</div>
<div>
<input name="subject-name" onchange="subjectChange()" type="checkbox" id="checkbox3" checked />
<label for="checkbox3">Germany</label>
</div>
<div>
<input name="subject-name" onchange="subjectChange()" type="checkbox" id="checkbox4" checked />
<label for="checkbox4">France </label>
</div>
<div>
<input name="subject-name" onchange="subjectChange()" type="checkbox" id="checkbox5" checked />
<label for="checkbox5">Italy</label>
</div>
<div>
<input name="subject-name" onchange="subjectChange()" type="checkbox" id="checkbox6" checked />
<label for="checkbox6">China</label>
</div>
<div>
<input name="subject-name" onchange="subjectChange()" type="checkbox" id="checkbox7" checked />
<label for="checkbox7">Japan</label>
</div>
<div>
<input name="subject-name" onchange="subjectChange()" type="checkbox" id="checkbox8" checked />
<label for="checkbox8">India</label>
</div>
</div>
<form action="">
<div class="select-box">
<div class="options-container">
<label class="option">
<input type="radio" class="radio first" value="5" name="question">
<span>5</span>
</label>
<label class="option">
<input type="radio" class="radio first" value="10" name="question">
<span>10</span>
</label>
<label class="option">
<input type="radio" class="radio first" value="15" name="question" >
<span>15</span>
</label>
<label class="option">
<input type="radio" class="radio first" value="20" name="question">
<span>20</span>
</label>
</div>
<div class="selected">Number of question</div>
</div>
<div class="select-box">
<div id="second-list" class="options-container">
<label class="option">
<input type="radio" class="radio second" value="10" name="minutes">
<span>10 minutes</span>
</label>
<label class="option">
<input type="radio" class="radio second" value="20" name="minutes" >
<span>20 minutes</span>
</label>
<label class="option">
<input type="radio" class="radio second" value="30" name="minutes">
<span>30 minutes</span>
</label>
<label class="option">
<input type="radio" class="radio second" value="40" name="minutes">
<span>40 minutes</span>
</label>
</div>
<div id="selected" class="selected">Select Exam Minute</div>
</div>
<button type="submit">Submit</button>
</form>
?
In your javascript you have added eventListeners to all the select options when your page is loaded at first. But when you call the 'subjectChange' function, the innerHTMl is changed and new elements are added to your page but these new elements do not have eventListeners attached to them. You need to attach eventListeners to these elements as well. Like this :
function subjectChange (){
var subjectName = document.getElementsByName('subject-name');
var questionSel = document.getElementsByClassName('options-container')[0];
var checkBoxlength = 0;
for(var i = 0; i < subjectName.length; i++) {
if(subjectName[i].checked){
checkBoxlength++
}
}
if(checkBoxlength <= 3) {
questionSel.innerHTML = `
<label class="option">
<input type="radio" class="radio first" value="5" name="question" checked>
<span>5</span>
< /label>
<label class="option">
<input type="radio" class="radio first" value="10" name="question" checked>
<span>10</span>
</label>
`;
} else if(checkBoxlength >= 4) {
questionSel.innerHTML = `
<label class="option">
<input type="radio" class="radio first" value="5" name="question" checked>
<span>5</span>
</label>
<label class="option">
<input type="radio" class="radio first" value="10" name="question" checked>
<span>10</span>
</label>
<label class="option">
<input type="radio" class="radio first" value="15" name="question" checked>
<span>15</span>
</label>
`;
}
start(); //this will add eventListeners to all the elements again
}
function start(){
const selectBox = document.querySelectorAll('.select-box')
for(var i = 0; i < selectBox.length; i++) {
const selected = selectBox[i].querySelector('.selected');
const optionsContainer = selectBox[i].querySelector('.options-container');
const optionList = selectBox[i].querySelectorAll('.option');
selected.addEventListener('click', function(){
optionsContainer.classList.toggle("active");
})
optionList.forEach( o => {
var optionListItem= o.querySelectorAll('input[type="radio"]:checked + span');
optionListItem.forEach(e => selected.innerHTML = e.innerHTML)
o.addEventListener('click', function(){
var optionListItem= o.querySelectorAll('input[type="radio"]:checked + span');
optionListItem.forEach(e => selected.innerHTML = e.innerHTML)
optionsContainer.classList.remove('active')
var firstOptionList = document.querySelector('input[type="radio"]:checked');
var secondOptionList = document.querySelectorAll('.radio.second');
secondOptionList.forEach(e => {
e.removeAttribute('checked')
if(e.value == (firstOptionList.value * 2)){
//e.checked = true;
e.setAttribute('checked','');
var optionListItem2= document.querySelectorAll('input[type="radio"]:checked + span');
const selectedTwo = document.getElementsByClassName('selected');
optionListItem2.forEach(e => selectedTwo[1].innerHTML = e.innerHTML);
console.log(e)
}
})
})
})
}
}
start();

jQuery not reacting to number input

I have this code, where if I fill in specific input fields then it will 'enable' or in this case remove a class but when it comes to filling in an input field of a number it won't react to the input value of the number what is filled. I want it to when something is filled in then enable the button.
I'm setting up a validation that when not all the fields are filled in, you can't go to the next page but before that you need to fill in all the fields.
HTML
<input id="LAMINAAT" type="radio" name="group1" onclick="myFunction()"
value="Laminaat" />
<label for="LAMINAAT">Laminaat</label>
<input id="PARKET" type="radio" name="group1" onclick="myFunction()" value="Parket" />
<label for="PARKET">Parket</label>
<input id="PVC" type="radio" name="group1" onclick="myFunction()" value="Pvc" />
<label for="PVC">PVC</label>
<hr>
<input id="JA2" type="radio" name="group3" value="Ja">
<label for="JA2" class="form-field__radio__label">Ja, meerprijs €1.50 per m<sup>2</sup></label><br>
<input id="NEE2" type="radio" name="group3" onclick="JaNeeFirst()" value="Nee">
<label for="NEE2">Nee</label>
<div id="form_JA2" class="desc desc3" style="float: inherit;">
<h5>Hoeveel m<sup>2</sup> ondervloer wil je laten leggen?</h5>
<input type="number" id="ondervloer" name="ondervloeren">
</div>
<hr>
<input id="JA3" type="radio" name="group4" value="Ja">
<label for="JA3" class="form-field__radio__label">Ja</label><br>
<input id="NEE3" type="radio" name="group4" onclick="JaNeeSecond()" value="Nee">
<label for="NEE3">Nee</label>
<hr>
<input id="JA4" type="radio" name="group5" value="Ja">
<label for="JA4" class="form-field__radio__label">Ja, meerprijs €5.00 per meter</label><br>
<input id="NEE4" type="radio" name="group5" onclick="JaNeeThirth()" value="Nee">
<label for="NEE4">Nee</label>
<hr>
<input id="JA5" type="radio" name="group6" value="Ja">
<label for="JA5" class="form-field__radio__label">Ja, meerprijs €2.50 per m<sup>2</sup></label><br>
<input id="NEE5" type="radio" name="group6" onclick="JaNeeFourth()" value="Nee">
<label for="NEE5">Nee</label>
<hr>
<input id="JA6" type="radio" name="group7" value="Ja">
<label for="JA6" class="form-field__radio__label">Ja, meerprijs €20.00 per deur</label><br>
<input id="NEE6" type="radio" name="group7" onclick="JaNeeFifth()" value="Nee">
<label for="NEE6">Nee</label>
<hr>
<input id="JA7" type="radio" name="group8" value="Ja">
<label for="JA7" class="form-field__radio__label">Ja, meerprijs €20.00 per plint</label><br>
<input id="NEE7" type="radio" name="group8" onclick="JaNeeSixth()" value="Nee">
<label for="NEE7">Nee</label>
<hr>
<input id="tweedebutton" type="button" value="volgende stap" onclick="show_next('user_details','qualification','bar2'); topFunction()" />
jQuery
$(document).ready(function () {
$("#tweedebutton").addClass("disabledbuttonNext");
$('input[type="radio"]').on('change', function () {
if ($('#LAMINAAT').is(":checked") && $('input[name="group3"]').is(":checked") && $('input[name="group4"]').is(":checked") && $('input[name="group5"]').is(":checked") && $('input[name="group7"]').is(":checked") && $('input[name="group8"]').is(":checked") ) {
$("#tweedebutton").removeClass("disabledbuttonNext");
} else if ($('#PARKET').is(":checked") && $('input[name="group3"]').is(":checked") && $('input[name="group4"]').is(":checked") && $('input[name="group5"]').is(":checked") && $('input[name="group7"]').is(":checked") && $('input[name="group8"]').is(":checked") && $('input[name="group6"]').is(":checked") ){
$("#tweedebutton").removeClass("disabledbuttonNext");
} else if ($('#PVC').is(":checked") && $('input[name="group3"]').is(":checked") && $('input[name="group4"]').is(":checked") && $('input[name="group5"]').is(":checked") && $('input[name="group7"]').is(":checked") && $('input[name="group8"]').is(":checked") ) {
$("#tweedebutton").removeClass("disabledbuttonNext");
}
else{
$("#tweedebutton").addClass("disabledbuttonNext");
}
});
});
$(document).ready(function () {
$('input[type="radio"], input[type="number"]').on('change', function () {
if ( $('#JA2').is(":checked") && $('#ondervloer').val() == '' ) {
$("#tweedebutton").addClass("disabledbuttonNext");
}
});
});
CSS
.disabledbuttonNext {
pointer-events: none;
opacity: 0.5;
}
I want the result to be when I filled in everything and I filled something in the number input(example: '1') that it reacts to it immediately and enables the button or in this case adds a class.
Here's the code similar for your application.
It interacts with both "radio buttons" as well as "input box" & accordingly triggers the "Submit button".
function checkout() {
alert("It worked");
}
$(document).ready(function() {
//console.log("Document loaded !");
$("input").change(function() {
var snacksValue = $('input[name="snacks"]:checked').val();
var extrasValue = $('input[name="extras"]:checked').val();
var quantity = $('#input1').val();
if (snacksValue != undefined && extrasValue != undefined && quantity != '') {
//console.log("go");
$('#checkout').removeClass("disabled");
$('#checkout').addClass("enabled");
} else {
//console.log("error");
$('#checkout').removeClass("enabled");
$('#checkout').addClass("disabled");
}
});
});
div {
margin: 10px 5px;
}
.enabled {
background-color: #4CAF50;
/* Green */
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
}
.disabled {
background-color: #e7e7e7;
border: none;
color: black;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
pointer-events: none;
/* display:none; */
/* If you wanna hide it */
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<label>
<input type="radio" name="snacks" value="burger" />
Burger</label>
<label>
<input type="radio" name="snacks" value="pizza" />Pizza</label>
<label>
<input type="radio" name="snacks" value="hotdog" /> Hotdog</label>
</div>
<div>
<label>
<input id="extra1" type="radio" name="extras" value="cheese">
With Cheese</label>
<label>
<input id="extra2" type="radio" name="extras" value="nocheese">
Without Cheese</label>
</div>
<div>
<label>Quantity</label>
<input id="input1" type="number" value="" min=1>
</div>
<div>
<input id="checkout" class="disabled" type="button" value="Next" onclick="checkout();" />
</div>

How to count number of checkboxes selected, change background after selected, and have hover continue to work

I want to create a list of checkboxes that users can select, however, limit the number of checkboxes to 5, as well as show the user how many they have currently clicked.
I also want to change the background color of the checkbox labels after they have been selected.
My main problem is that the number showing how many checkboxes have been selected is always one click behind. Also, the background color is changing after being selected, but the hover call stops working if selected.
Finally, I'd love to hear any suggestions on how to make my count function cleaner. I don't like having 7 if statements...
$(document).ready(function() {
$("input[name='group_option[]']").change(function() {
var maxAllowed = 5;
var cnt = $("input[name='group_option[]']:checked").length;
if (cnt > maxAllowed) {
$(this).prop("checked", "");
}
});
});
function count() {
var count = 0;
if ($('#checkbox1').is(':checked')) {
count = count + 1;
}
if ($('#checkbox2').is(':checked')) {
count = count + 1;
}
if ($('#checkbox3').is(':checked')) {
count = count + 1;
}
if ($('#checkbox4').is(':checked')) {
count = count + 1;
}
if ($('#checkbox5').is(':checked')) {
count = count + 1;
}
if ($('#checkbox6').is(':checked')) {
count = count + 1;
}
if ($('#checkbox7').is(':checked')) {
count = count + 1;
}
document.getElementById("count").innerHTML = count + "/5 Selected";
}
.options {
background-color: #e6e6e6;
display: block;
width: 300px;
margin-left: 20px;
padding: 2px;
margin-bottom: 1px;
}
.options:hover {
color: black;
cursor: pointer;
transition-duration: .15s;
background-color: #b3b3b3;
}
input {
float: left;
}
label:hover {
background-color: #bfbfbf;
}
input[type=checkbox]:checked + label {
background-color: #cccccc;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<b id="count" style="float: left;">0/5 Selected</b>
<br>
<br>
<input id="checkbox1" type="checkbox" name="group_option[]" value="option1" />
<label for="checkbox1" class="options" onclick="count(this)"> Option 1</label>
<input id="checkbox2" type="checkbox" name="group_option[]" value="option2" />
<label for="checkbox2" class="options" onclick="count(this)"> Option 2</label>
<input id="checkbox3" type="checkbox" name="group_option[]" value="option3" />
<label for="checkbox3" class="options" onclick="count(this)"> Option 3</label>
<input id="checkbox4" type="checkbox" name="group_option[]" value="option4" />
<label for="checkbox4" class="options" onclick="count(this)"> Option 4</label>
<input id="checkbox5" type="checkbox" name="group_option[]" value="option5" />
<label for="checkbox5" class="options" onclick="count(this)"> Option 5</label>
<input id="checkbox6" type="checkbox" name="group_option[]" value="option6" />
<label for="checkbox6" class="options" onclick="count(this)"> Option 6</label>
<input id="checkbox7" type="checkbox" name="group_option[]" value="option7" />
<label for="checkbox7" class="options" onclick="count(this)"> Option 7</label>
There's no need for your separate count() function as you can do all the required processing in your jQuery change event handler (and on* event attributes are considered outdated and should avoided anyway). You already have the cnt variable stored there which you can use. Try this:
$(document).ready(function() {
var maxAllowed = 5;
$("input[name='group_option[]']").change(function() {
var cnt = $("input[name='group_option[]']:checked").length;
if (cnt > maxAllowed)
$(this).prop("checked", false);
else
$('#count').text(cnt + '/5 Selected');
});
});
.options {
background-color: #e6e6e6;
display: block;
width: 300px;
margin-left: 20px;
padding: 2px;
margin-bottom: 1px;
}
.options:hover {
color: black;
cursor: pointer;
transition-duration: .15s;
background-color: #b3b3b3;
}
input {
float: left;
}
input:checked + label {
background-color: #cccccc;
}
input:checked + label:hover {
background-color: #bfbfbf;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<b id="count" style="float: left;">0/5 Selected</b><br><br>
<input id="checkbox1" type="checkbox" name="group_option[]" value="option1" />
<label for="checkbox1" class="options"> Option 1</label>
<input id="checkbox2" type="checkbox" name="group_option[]" value="option2" />
<label for="checkbox2" class="options"> Option 2</label>
<input id="checkbox3" type="checkbox" name="group_option[]" value="option3" />
<label for="checkbox3" class="options"> Option 3</label>
<input id="checkbox4" type="checkbox" name="group_option[]" value="option4" />
<label for="checkbox4" class="options"> Option 4</label>
<input id="checkbox5" type="checkbox" name="group_option[]" value="option5" />
<label for="checkbox5" class="options"> Option 5</label>
<input id="checkbox6" type="checkbox" name="group_option[]" value="option6" />
<label for="checkbox6" class="options"> Option 6</label>
<input id="checkbox7" type="checkbox" name="group_option[]" value="option7" />
<label for="checkbox7" class="options"> Option 7</label>
because of the CSS tag and for the anecdote, here is a CSS possibility :
// no need of javascript here, it is a CSS demo
form {
display: table;
}
label {
display: block;
margin-left: 20px;
position: relative;
padding: 0.25em 1em 0 0;
background: lightgray;
margin-bottom: 1px;
}
label[for^="checkbox"]:after {/* select the labels to use to draw a checkbox*/
content: '';
position: absolute;
right: 100%;
display: inline-block;
line-height: 9px;
height: 11px;
width: 11px;
margin: 2px 5px 0 0;
border: solid 1px #999;
box-shadow: inset 0 0 0 1px white, inset 0 0 1px 1px gray;
background: linear-gradient(to bottom right, gray, white 75%)
}
/* update checkbox colors on hover/checked */
#checkbox1:checked ~ label[for="checkbox1"]:after,
#checkbox2:checked ~ label[for="checkbox2"]:after,
#checkbox3:checked ~ label[for="checkbox3"]:after,
#checkbox4:checked ~ label[for="checkbox4"]:after,
#checkbox5:checked ~ label[for="checkbox5"]:after,
#checkbox6:checked ~ label[for="checkbox6"]:after,
#checkbox7:checked ~ label[for="checkbox7"]:after,
label:hover:after {
border: solid 1px #5586A3;
box-shadow: inset 0 0 0 1px white, inset 0 0 0 2px #9FD7F9;
background: linear-gradient(to bottom right, #7AB6DB, white 75%)
}
/* about time to hide imputs cloned in CSS */
[name^="group_option"] {
position: absolute;
right: 100%;
}
/* trigger the checkmark when checked */
#checkbox1:checked ~ label[for="checkbox1"]:after,
#checkbox2:checked ~ label[for="checkbox2"]:after,
#checkbox3:checked ~ label[for="checkbox3"]:after,
#checkbox4:checked ~ label[for="checkbox4"]:after,
#checkbox5:checked ~ label[for="checkbox5"]:after,
#checkbox6:checked ~ label[for="checkbox6"]:after,
#checkbox7:checked ~ label[for="checkbox7"]:after {
content: '\2714';
color: #223C82;
}
/* disallow option when 5 is reached */
[name^="group_option"]:checked ~[name^="group_option"]:checked ~[name^="group_option"]:checked ~[name^="group_option"]:checked ~[name^="group_option"]:checked ~ label {
pointer-events:none;
color:gray;
}
/* but allow to unchecked if you change yor mind */
label:hover,
#checkbox1:checked ~ label[for="checkbox1"],
#checkbox2:checked ~ label[for="checkbox2"],
#checkbox3:checked ~ label[for="checkbox3"],
#checkbox4:checked ~ label[for="checkbox4"],
#checkbox5:checked ~ label[for="checkbox5"],
#checkbox6:checked ~ label[for="checkbox6"],
#checkbox7:checked ~ label[for="checkbox7"] {
pointer-events:auto;
color:initial;
background: gray;
cursor:pointer;
}
/* add infos */
b {
display: block;
text-align: center
}
form {
counter-reset: checked;
}
input:checked {
counter-increment: checked;
}
b:before {
content: counter(checked);
}
b:after {
content: '5'
}
<form>
<!-- input linked to labels to be hidden -->
<input id="checkbox1" type="checkbox" name="group_option[]" value="option1" />
<input id="checkbox2" type="checkbox" name="group_option[]" value="option2" />
<input id="checkbox3" type="checkbox" name="group_option[]" value="option3" />
<input id="checkbox4" type="checkbox" name="group_option[]" value="option4" />
<input id="checkbox5" type="checkbox" name="group_option[]" value="option5" />
<input id="checkbox6" type="checkbox" name="group_option[]" value="option6" />
<input id="checkbox7" type="checkbox" name="group_option[]" value="option7" />
<!-- end hidden input linked to labels -->
<b>/</b>
<!-- label using pseudo to draw the checkbox -->
<label for="checkbox1" class="options"> Option 1</label>
<label for="checkbox2" class="options"> Option 2</label>
<label for="checkbox3" class="options" > Option 3</label>
<label for="checkbox4" class="options"> Option 4</label>
<label for="checkbox5" class="options"> Option 5</label>
<label for="checkbox6" class="options"> Option 6</label>
<label for="checkbox7" class="options"> Option 7</label>
<!-- end label using pseudo to draw the checkbox -->
<form>
demo to play with
The answer is at line 4 of your own code...
function count() {
var cnt = $("input[name='group_option[]']:checked").length;
document.getElementById("count").innerHTML = cnt + "/5 Selected";
}
To answer the second part of your question first: the reason your losing the :hover state on the labels is due to CSS specificity; you have the rule for a label following a :checked input after the rule for a hovered label, so the latter is being overridden. But the selector for the :checked rule has a higher specficity than the one for the :hover rule so simply changing the order of those two rules won't be enough, you'll also need to increase the specificity of the :hover rule.
The rest of your problems can be solved by addressing the last part of your question; simplifying your JavaScript. One way to do so would be to loop through all the inputs and listen for the change event on each, incrementing or decrementing the value of the count variable, depending on the checbox's status and the checking the value of count is not greater than 5.
I've added the ability to have some checkboxes initially checked and also taken the liberty of simplfying how you update the counter element and adding some CSS to make unchecked checkboxes & their labels appear disabled once 5 options have been selected in order to make it clearer that no more options can be selected.
var inputs=document.querySelectorAll("input.options"),
length=inputs.length,
counter=document.querySelector("#count>span"),
dataset=counter.parentNode.dataset,
count=0,input,max=5;
while(length--){
input=inputs[length];
count+=input.checked;
input.addEventListener("change",function(event){
count+=this.checked&&1||-1;
if(count>max){
this.checked=0;
count--;
}
dataset.count=counter.innerHTML=count;
},0);
}
dataset.count=counter.innerHTML=count;
*{box-sizing:border-box;font-family:sans-serif;}
#count{
font-weight:bold;
margin:0 0 20px;
}
input.options{
clear:left;
float:left;
}
label.options{
background:#e6e6e6;
cursor:pointer;
display:block;
margin:0 0 1px 20px;
padding:2px 2px 2px 20px;
transition:background .15s;
width:300px;
}
input.options:checked+label.options{
background:#ccc;
}
input.options+label.options:hover{
background:#bfbfbf;
}
#count[data-count="5"]~input.options:not(:checked),#count[data-count="5"]~input.options:not(:checked)+label.options{
opacity:.5;
pointer-events:none;
}
<p id="count"><span>0</span>/5 Selected</p>
<input class="options" id="checkbox1" name="group_option[]" type="checkbox" value="option1" />
<label class="options" for="checkbox1">Option 1</label>
<input class="options" id="checkbox2" name="group_option[]" type="checkbox" value="option2" />
<label class="options" for="checkbox2">Option 2</label>
<input class="options" id="checkbox3" name="group_option[]" type="checkbox" value="option3" />
<label class="options" for="checkbox3">Option 3</label>
<input checked class="options" id="checkbox4" name="group_option[]" type="checkbox" value="option4" />
<label class="options" for="checkbox4">Option 4</label>
<input class="options" id="checkbox5" name="group_option[]" type="checkbox" value="option5" />
<label class="options" for="checkbox5">Option 5</label>
<input class="options" id="checkbox6" name="group_option[]" type="checkbox" value="option6" />
<label class="options" for="checkbox6">Option 6</label>
<input class="options" id="checkbox7" name="group_option[]" type="checkbox" value="option7" />
<label class="options" for="checkbox7">Option 7</label>

Categories

Resources