1
1
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 ( ) ;
18
23
} ) ;
19
24
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 ) ;
26
32
} ) ;
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
+
27
42
console . log ( "Answers saved to local storage." ) ;
28
43
}
29
44
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 ;
36
52
} ) ;
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
+
37
62
console . log ( "Answers loaded from local storage." ) ;
38
63
}
39
64
@@ -72,7 +97,7 @@ window.addEventListener("load", function() {
72
97
} ) ;
73
98
74
99
75
- // Function to download the answers as a PDF with a more professional layout
100
+ // Function to download the answers as a PDF
76
101
document . getElementById ( 'downloadBtn' ) . addEventListener ( 'click' , function ( ) {
77
102
const questions = document . querySelectorAll ( '.question' ) ;
78
103
let answersHTML = `
@@ -81,24 +106,25 @@ document.getElementById('downloadBtn').addEventListener('click', function() {
81
106
<h2 style="font-size: 18px;">Submitter</h2>
82
107
<table>` ;
83
108
109
+ // Include table data
84
110
const table = document . querySelector ( 'table.personal-details' ) ;
85
111
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>` ;
87
113
}
88
114
answersHTML += `</table>` ;
89
115
90
116
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 ;
94
120
95
121
answersHTML += `
96
122
<h2 style="font-size: 18px; margin-bottom: 5px;">${ questionText } </h2>
97
123
<p style="font-size: 14px;">${ answerText || 'No answer provided' } </p>
98
124
` ;
99
125
} ) ;
100
126
101
- answersHTML += `</div>` ; // Closing the div
127
+ answersHTML += `</div>` ;
102
128
103
129
const opt = {
104
130
margin : 1 ,
0 commit comments