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();
Related
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>
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;
});
I am trying to add a CSS class to a JavaScript created element, but am experiencing a reference error for the trackerMarker element. The tracker-marker DIVs are displaying on load, but are undefined when used in conjunction with the event listener.
I have tried using the createElement function inside and outside of the for loop.
I am not sure where the issue is. I am learning JS as I go, so any tips or suggestions are greatly appreciated.
function answerTracker() {
const answerTracker = document.querySelector(".tracker");
let questions = document.querySelectorAll(".container");
for (let i = 0; i < questions.length; i++) {
let trackerMarker = document.createElement("div");
trackerMarker.className = "tracker-marker";
answerTracker.appendChild(trackerMarker);
}
}
const runQuiz = (() => {
// Generate tracker
answerTracker();
let answers = document.querySelectorAll(".radio");
answers.forEach(function(answer) {
answer.addEventListener("click", (e) => {
let target = e.target;
let container = target.closest(".container");
let question = container.querySelector(".question");
let next = container.querySelector("button");
if (e.target.value == "t") {
trackerMarker.classList.add("correct");
} else {
trackerMarker.classList.add("incorrect");
}
if (next !== null && next !== "") {
next.style.display = "block";
}
question.style.display = "none";
});
});
})();
.tracker {
display: flex;
padding: 50px 0;
}
.tracker .tracker-marker {
width: 28px;
height: 28px;
margin: 0 1.25rem;
background-color: red;
border-radius: 100%;
}
.tracker .tracker-marker.correct {
background-color: blue;
}
.question {
margin-bottom: 18px;
}
.question .question__label {
margin-bottom: 8px;
}
<div class="tracker">
</div>
<fieldset class="container">
<div class="question">
<div class="question__label">
<span class="question-marker">1</span>
<label class="formControlLabel">Question One</label>
</div>
<input type="radio" name="form[question-one]" value="t" id="question-one0" class="radio">
<label for="question-one0">Answer A</label>
<br>
<input type="radio" name="form[question-one]" value="f" id="question-one1" class="radio">
<label for="question-one1">Answer B</label>
<br>
<input type="radio" name="form[question-one]" value="f" id="question-one2" class="radio">
<label for="question-one2">Answer C</label>
<br>
<input type="radio" name="form[question-one]" value="f" id="question-one3" class="radio">
<label for="question-one3">Answer D</label>
<button type="button" style="display: none;">Next</button>
</div>
</fieldset>
<fieldset class="container" style="display: none;">
<div class="question">
<div class="question__label">
<span class="question-marker">2</span>
<label class="formControlLabel">Question Two</label>
</div>
<input type="radio" name="form[question-two]" value="t" id="question-two0" class="radio">
<label for="question-two0">Answer A</label>
<br>
<input type="radio" name="form[question-two]" value="f" id="question-two1" class="radio">
<label for="question-two1">Answer B</label>
<br>
<input type="radio" name="form[question-two]" value="f" id="question-two2" class="radio">
<label for="question-two2">Answer C</label>
<br>
<input type="radio" name="form[question-two]" value="f" id="question-two3" class="radio">
<label for="question-two3">Answer D</label>
<button type="button" style="display: none;">Next</button>
</div>
</fieldset>
<fieldset class="container" style="display: none;">
<div class="question">
<div class="question__label">
<span class="question-marker">3</span>
<label class="formControlLabel">Question Three</label>
</div>
<input type="radio" name="form[question-three]" value="t" id="question-three0" class="radio">
<label for="question-three0">Answer A</label>
<br>
<input type="radio" name="form[question-three]" value="f" id="question-three1" class="radio">
<label for="question-three1">Answer B</label>
<br>
<input type="radio" name="form[question-three]" value="f" id="question-three2" class="radio">
<label for="question-three2">Answer C</label>
<br>
<input type="radio" name="form[question-three]" value="f" id="question-three3" class="radio">
<label for="question-three3">Answer D</label>
<button type="button" style="display: none;">Next</button>
</div>
</fieldset>
You never set trackerMarker inside runQuiz.
I used getElementsByClassName (because how you wrote your code) which will return an array of items which is not ideal. You should add an ID to your trackerMarker div and then use getElementById.
function answerTracker() {
const answerTracker = document.querySelector(".tracker");
let questions = document.querySelectorAll(".container");
for (let i = 0; i < questions.length; i++) {
let trackerMarker = document.createElement("div");
trackerMarker.className = "tracker-marker";
answerTracker.appendChild(trackerMarker);
}
}
const runQuiz = (() => {
// Generate tracker
answerTracker();
let answers = document.querySelectorAll(".radio");
answers.forEach(function(answer) {
answer.addEventListener("click", (e) => {
let target = e.target;
let container = target.closest(".container");
let question = container.querySelector(".question");
let next = container.querySelector("button");
let trackerMarker = document.getElementsByClassName('tracker-marker')[0];
// ^-- I added this line only
if (e.target.value == "t") {
trackerMarker.classList.add("correct");
} else {
trackerMarker.classList.add("incorrect");
}
if (next !== null && next !== "") {
next.style.display = "block";
}
question.style.display = "none";
});
});
})();
.tracker {
display: flex;
padding: 50px 0;
}
.tracker .tracker-marker {
width: 28px;
height: 28px;
margin: 0 1.25rem;
background-color: red;
border-radius: 100%;
}
.tracker .tracker-marker.correct {
background-color: blue;
}
.question {
margin-bottom: 18px;
}
.question .question__label {
margin-bottom: 8px;
}
<div class="tracker">
</div>
<fieldset class="container">
<div class="question">
<div class="question__label">
<span class="question-marker">1</span>
<label class="formControlLabel">Question One</label>
</div>
<input type="radio" name="form[question-one]" value="t" id="question-one0" class="radio">
<label for="question-one0">Answer A</label>
<br>
<input type="radio" name="form[question-one]" value="f" id="question-one1" class="radio">
<label for="question-one1">Answer B</label>
<br>
<input type="radio" name="form[question-one]" value="f" id="question-one2" class="radio">
<label for="question-one2">Answer C</label>
<br>
<input type="radio" name="form[question-one]" value="f" id="question-one3" class="radio">
<label for="question-one3">Answer D</label>
<button type="button" style="display: none;">Next</button>
</div>
</fieldset>
<fieldset class="container" style="display: none;">
<div class="question">
<div class="question__label">
<span class="question-marker">2</span>
<label class="formControlLabel">Question Two</label>
</div>
<input type="radio" name="form[question-two]" value="t" id="question-two0" class="radio">
<label for="question-two0">Answer A</label>
<br>
<input type="radio" name="form[question-two]" value="f" id="question-two1" class="radio">
<label for="question-two1">Answer B</label>
<br>
<input type="radio" name="form[question-two]" value="f" id="question-two2" class="radio">
<label for="question-two2">Answer C</label>
<br>
<input type="radio" name="form[question-two]" value="f" id="question-two3" class="radio">
<label for="question-two3">Answer D</label>
<button type="button" style="display: none;">Next</button>
</div>
</fieldset>
<fieldset class="container" style="display: none;">
<div class="question">
<div class="question__label">
<span class="question-marker">3</span>
<label class="formControlLabel">Question Three</label>
</div>
<input type="radio" name="form[question-three]" value="t" id="question-three0" class="radio">
<label for="question-three0">Answer A</label>
<br>
<input type="radio" name="form[question-three]" value="f" id="question-three1" class="radio">
<label for="question-three1">Answer B</label>
<br>
<input type="radio" name="form[question-three]" value="f" id="question-three2" class="radio">
<label for="question-three2">Answer C</label>
<br>
<input type="radio" name="form[question-three]" value="f" id="question-three3" class="radio">
<label for="question-three3">Answer D</label>
<button type="button" style="display: none;">Next</button>
</div>
</fieldset>
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>
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>