Skip to content

Commit d0fa65a

Browse files
committed
Updated answers.js
1 parent cbb1ee0 commit d0fa65a

File tree

1 file changed

+60
-34
lines changed

1 file changed

+60
-34
lines changed

javascripts/answers.js

+60-34
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,64 @@
11
window.addEventListener("load", function() {
2-
var questions = document.getElementsByClassName("question");
3-
console.log(questions);
4-
for (const [index, element] of Array.from(questions).entries()) {
5-
// <textarea id="question27" placeholder="Type your answer here..." class="input-box"></textarea>
6-
const ansElem = document.createElement("textarea");
7-
ansElem.setAttribute("id", "question" + (index + 1));
8-
ansElem.setAttribute("placeholder", "Type your answer here...");
9-
ansElem.classList.add("input-box");
10-
element.insertAdjacentElement("afterend", ansElem);
11-
// Automatically resize textareas based on input
12-
ansElem.addEventListener('input', function() {
13-
this.style.height = 'auto';
14-
this.style.height = (this.scrollHeight) + 'px';
15-
saveAnswers();
16-
});
17-
};
2+
// Process .question elements to add corresponding inputs
3+
const questions = document.getElementsByClassName("question");
4+
for (const [index, element] of Array.from(questions).entries()) {
5+
const ansElem = document.createElement("textarea");
6+
ansElem.setAttribute("id", "question" + (index + 1));
7+
ansElem.setAttribute("placeholder", "Type your answer here...");
8+
ansElem.classList.add("input-box");
9+
element.insertAdjacentElement("afterend", ansElem);
10+
11+
// Save answers on input change
12+
ansElem.addEventListener("input", saveAnswers);
13+
}
14+
15+
// Save table inputs
16+
const tableInputs = document.querySelectorAll("table input[type='text']");
17+
tableInputs.forEach(input => {
18+
input.addEventListener("input", saveAnswers);
19+
});
20+
21+
// Load previously saved answers
22+
loadAnswers();
1823
});
1924

20-
// Function to save answers to localStorage
21-
function saveTableAnswers() {
22-
const inputs = document.querySelectorAll('table input[type="text"]');
23-
inputs.forEach(input => {
24-
const value = input.value || ''; // Get the input value or an empty string
25-
localStorage.setItem(input.id, value); // Save value in localStorage using the input's ID
25+
// Function to save all answers (textarea and table inputs)
26+
function saveAnswers() {
27+
// Save textareas
28+
const textareas = document.querySelectorAll('textarea[id^="question"]');
29+
textareas.forEach(textarea => {
30+
const answer = textarea.value || '';
31+
localStorage.setItem(textarea.id, answer);
2632
});
33+
34+
// Save table inputs
35+
const tableInputs = document.querySelectorAll("table input[type='text']");
36+
tableInputs.forEach(input => {
37+
const answer = input.value || '';
38+
const inputId = input.id || `table-input-${Array.from(tableInputs).indexOf(input)}`;
39+
localStorage.setItem(inputId, answer);
40+
});
41+
2742
console.log("Answers saved to local storage.");
2843
}
2944

30-
// Load answers from localStorage
31-
function loadTableAnswers() {
32-
const inputs = document.querySelectorAll('table input[type="text"]');
33-
inputs.forEach(input => {
34-
const savedValue = localStorage.getItem(input.id) || ''; // Load the saved value or an empty string
35-
input.value = savedValue; // Set the value to the input field
45+
// Function to load saved answers
46+
function loadAnswers() {
47+
// Load textareas
48+
const textareas = document.querySelectorAll('textarea[id^="question"]');
49+
textareas.forEach(textarea => {
50+
const savedAnswer = localStorage.getItem(textarea.id) || '';
51+
textarea.value = savedAnswer;
3652
});
53+
54+
// Load table inputs
55+
const tableInputs = document.querySelectorAll("table input[type='text']");
56+
tableInputs.forEach(input => {
57+
const inputId = input.id || `table-input-${Array.from(tableInputs).indexOf(input)}`;
58+
const savedAnswer = localStorage.getItem(inputId) || '';
59+
input.value = savedAnswer;
60+
});
61+
3762
console.log("Answers loaded from local storage.");
3863
}
3964

@@ -72,7 +97,7 @@ window.addEventListener("load", function() {
7297
});
7398

7499

75-
// Function to download the answers as a PDF with a more professional layout
100+
// Function to download the answers as a PDF
76101
document.getElementById('downloadBtn').addEventListener('click', function() {
77102
const questions = document.querySelectorAll('.question');
78103
let answersHTML = `
@@ -81,24 +106,25 @@ document.getElementById('downloadBtn').addEventListener('click', function() {
81106
<h2 style="font-size: 18px;">Submitter</h2>
82107
<table>`;
83108

109+
// Include table data
84110
const table = document.querySelector('table.personal-details');
85111
for (var i = 0, row; row = table.rows[i]; i++) {
86-
answersHTML += `<tr><td style="font-weight: bold;">${row.cells[0].textContent}</td><td>${row.cells[1].children[0].value}</td></tr>`;
112+
answersHTML += `<tr><td style="font-weight: bold;">${row.cells[0].textContent}</td><td>${row.cells[1].children[0].value}</td></tr>`;
87113
}
88114
answersHTML += `</table>`;
89115

90116
questions.forEach((question, index) => {
91-
const questionNumber = index + 1; // Incrementing index for question number
92-
const questionText = question.innerHTML; // Get the question text
93-
const answerText = document.getElementById(`question${questionNumber}`).value; // Get the corresponding answer
117+
const questionNumber = index + 1;
118+
const questionText = question.innerHTML;
119+
const answerText = document.getElementById(`question${questionNumber}`).value;
94120

95121
answersHTML += `
96122
<h2 style="font-size: 18px; margin-bottom: 5px;">${questionText}</h2>
97123
<p style="font-size: 14px;">${answerText || 'No answer provided'}</p>
98124
`;
99125
});
100126

101-
answersHTML += `</div>`; // Closing the div
127+
answersHTML += `</div>`;
102128

103129
const opt = {
104130
margin: 1,

0 commit comments

Comments
 (0)