I have created JS/Html page for survey but i want to show the result of survey page in table format after clicking submit button. Right now it is not coming as a table format, the answer is coming section-wise. I want one final answer which come as a table format showing result of individual section and total of both the section. Thanks in advance
Js File :
function displayRadioValue() { let section1 = document.querySelectorAll('.section-1 > input[type="radio"]') let section2 = document.querySelectorAll('.section-2 > input[type="radio"]') let section1Total = 0 let section2Total = 0 let section1Question = 0 let section2Question = 0 let section1Res = document.querySelector('.section-1-results') let section2Res = document.querySelector('.section-2-results') let result1 = '' let result2 = '' //Section 1 section1.forEach(function(radio, index) { if (radio.checked) { section2Question++ section1Total += +radio.value } }) //Section 2 section2.forEach(function(radio, index) { if (radio.checked) { section1Question++ section2Total += +radio.value } }) //Section 1 result1 += "<b>Results:</b><br>" result1 += "Total: " + section1Total + "<br>" result1 += "Percentage: " + ((section1Total / (section2Question * 3)) * 100).toFixed(2) + "%" section1Res.innerHTML = result1 //Section 2 result2 += "<b>Results:</b><br>" result2 += "Total: " + section2Total + "<br>" result2 += "Percentage: " + ((section2Total / (section2Question * 3)) * 100).toFixed(2) + "%" section2Res.innerHTML = result2 }
Html Page :
<p> Select a radio button and click on Submit. </p> <div class="section-1"> <h2>Section 1</h2> question 1: <input type="radio" name="question1" value="1">1 <input type="radio" name="question1" value="2">2 <input type="radio" name="question1" value="3">3 <br> question 2: <input type="radio" name="question2" value="1">1 <input type="radio" name="question2" value="2">2 <input type="radio" name="question2" value="3">3 <br> question 3: <input type="radio" name="question3" value="1">1 <input type="radio" name="question3" value="2">2 <input type="radio" name="question3" value="3">3 </div> <br> <div class="section-1-results"></div> <div class="section-2"> <h2>Section 2</h2> question 1: <input type="radio" name="question4" value="1">1 <input type="radio" name="question4" value="2">2 <input type="radio" name="question4" value="3">3 <br> question 2: <input type="radio" name="question5" value="1">1 <input type="radio" name="question5" value="2">2 <input type="radio" name="question5" value="3">3 <br> question 3: <input type="radio" name="question6" value="1">1 <input type="radio" name="question6" value="2">2 <input type="radio" name="question6" value="3">3 <br> question 4: <input type="radio" name="question7" value="1">1 <input type="radio" name="question7" value="2">2 <input type="radio" name="question7" value="3">3 </div> <br> <div class="section-2-results"></div> <br> <button type="button" onclick="displayRadioValue()"> Submit </button>
Advertisement
Answer
You can simply make a dynamic table
in your JS function which returns
an each table of results
from each section.
I believe you want one final
answer of each
section
with results showing at the bottom of it.
I have also added some validation to make sure that user must select atleast
one question for each section so that the tables are not empty
all the time. It will show user an error message
if question is selected from each
section.
Instead of using two results
div i am only using one final-results
where each section
results table will be displayed
for you in a table format.
Live Demo:
function displayRadioValue() { let section1 = document.querySelectorAll('.section-1 > input[type="radio"]') let section2 = document.querySelectorAll('.section-2 > input[type="radio"]') let section1Total = 0 let section2Total = 0 let section1Question = 0 let section2Question = 0 let finalResults = document.querySelector('.final-results') let result1 = '' let result2 = '' finalResults.innerHTML = '' //Section 1 section1.forEach(function(radio, index) { if (radio.checked) { section2Question++ section1Total += +radio.value } }) //Section 2 section2.forEach(function(radio, index) { if (radio.checked) { section1Question++ section2Total += +radio.value } }) //Final Results and validation if (section1Total > 0 && section2Total > 0) { finalResults.innerHTML += genTable(section1Question, section1Total, 1) finalResults.innerHTML += genTable(section2Question, section2Total, 2) } else { finalResults.innerHTML = 'Snap! Please select the atleast one survey question from each section' } } function genTable(ques, total, section) { var result = "<b>Section " + section + ":</b><br>" var tr = "<tr><th>" + total + "</th><th>" + ((total / (ques * 3)) * 100).toFixed(2) + "</th></tr>" result += "<table><thead><tr><th>Total Score</th><th>Percentage</th></tr></thead><tbody>" + tr + "</tbody></table>" return result }
table, table tr th, table tr td { border: 1px solid black; }
<p> Select a radio button and click on Submit. </p> <div class="section-1"> <h2>Section 1</h2> question 1: <input type="radio" name="question1" value="1">1 <input type="radio" name="question1" value="2">2 <input type="radio" name="question1" value="3">3 <br> question 2: <input type="radio" name="question2" value="1">1 <input type="radio" name="question2" value="2">2 <input type="radio" name="question2" value="3">3 <br> question 3: <input type="radio" name="question3" value="1">1 <input type="radio" name="question3" value="2">2 <input type="radio" name="question3" value="3">3 </div> <div class="section-2"> <h2>Section 2</h2> question 1: <input type="radio" name="question4" value="1">1 <input type="radio" name="question4" value="2">2 <input type="radio" name="question4" value="3">3 <br> question 2: <input type="radio" name="question5" value="1">1 <input type="radio" name="question5" value="2">2 <input type="radio" name="question5" value="3">3 <br> question 3: <input type="radio" name="question6" value="1">1 <input type="radio" name="question6" value="2">2 <input type="radio" name="question6" value="3">3 <br> question 4: <input type="radio" name="question7" value="1">1 <input type="radio" name="question7" value="2">2 <input type="radio" name="question7" value="3">3 </div> <br> <div class="final-results"></div> <br> <button type="button" onclick="displayRadioValue()"> Submit </button>