|
74 | 74 | if (answerDiv && lastLabel && vars[lastLabel] !== undefined) { |
75 | 75 | answerDiv.textContent = "✅ Result: " + Number(vars[lastLabel]).toLocaleString(undefined, { maximumFractionDigits: 5 }); |
76 | 76 | } |
| 77 | + |
| 78 | + // Update all variable-value spans for this wrapper |
| 79 | + updateVariableValueSpans(wrapper, vars); |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + // Update all variable-value spans for this wrapper using current vars |
| 84 | + function updateVariableValueSpans(wrapper, vars) { |
| 85 | + if (!wrapper) return; |
| 86 | + wrapper.querySelectorAll('.qna-variable-value').forEach(function(el) { |
| 87 | + var name = el.getAttribute('data-var-name'); |
| 88 | + el.textContent = (vars && vars[name] !== undefined) ? vars[name] : ''; |
| 89 | + }); |
77 | 90 | } |
78 | | - // Calculate result on window load |
79 | | - window.addEventListener("DOMContentLoaded", function () { |
80 | | - calc_("{{ $wrapperId }}"); |
81 | | - }); |
82 | 91 |
|
83 | | - // To calculate the result automatically when the user edits a variable (contenteditable or input), |
84 | | - // add an event listener for input and change events on all [data-calc-var] elements. |
85 | | - {{/* window.addEventListener("DOMContentLoaded", function () { |
| 92 | + // Calculate result on window load and set up listeners |
| 93 | + window.addEventListener("DOMContentLoaded", function () { |
86 | 94 | calc_("{{ $wrapperId }}"); |
87 | | - // Listen for changes on all variable fields |
88 | 95 | var wrapper = document.getElementById("{{ $wrapperId }}"); |
89 | 96 | if (wrapper) { |
90 | 97 | wrapper.querySelectorAll("[data-calc-var]").forEach(function (el) { |
91 | 98 | el.addEventListener("input", function () { |
92 | 99 | calc_("{{ $wrapperId }}"); |
93 | 100 | }); |
94 | | - // For input elements, also listen to 'change' |
95 | 101 | el.addEventListener("change", function () { |
96 | 102 | calc_("{{ $wrapperId }}"); |
97 | 103 | }); |
98 | 104 | }); |
99 | 105 | } |
100 | | - }); */}} |
101 | | - |
102 | | - |
103 | | -// Store each variable's value to localStroage on input/change. |
104 | | -// On page load, restore values from localStroage (if present). |
105 | | - |
106 | | -// Helper to get a unique key for each variable in this calculator |
107 | | -function getVarKey(wrapperId, varName) { |
108 | | - return "calc_" + wrapperId + "_" + varName; |
109 | | -} |
110 | | - |
111 | | -{{/* function saveVarsToLocal(wrapperId) { |
112 | | - var wrapper = document.getElementById(wrapperId); |
113 | | - if (!wrapper) return; |
114 | | - wrapper.querySelectorAll("[data-calc-var]").forEach(function (el) { |
115 | | - var name = el.getAttribute("data-calc-var"); |
116 | | - var val = el.value !== undefined ? el.value : el.textContent; |
117 | | - localStorage.setItem(getVarKey(wrapperId, name), val); |
118 | 106 | }); |
119 | | -} */}} |
120 | | - |
121 | | -// And in your JS (in question.html), after every input/change, store the value in localStorage by variable name: |
122 | | - |
123 | | -function saveVarsToLocalByName(wrapperId) { |
124 | | - var wrapper = document.getElementById(wrapperId); |
125 | | - if (!wrapper) return; |
126 | | - wrapper.querySelectorAll("[data-calc-var]").forEach(function (el) { |
127 | | - var name = el.getAttribute("data-calc-var"); |
128 | | - var val = el.value !== undefined ? el.value : el.textContent; |
129 | | - localStorage.setItem("qna_var_" + name, val); |
130 | | - }); |
131 | | -} |
132 | | - |
133 | | -// Call this function whenever a variable is updated: |
134 | | -window.addEventListener("DOMContentLoaded", function () { |
135 | | - var wrapper = document.getElementById("{{ $wrapperId }}"); |
136 | | - if (wrapper) { |
137 | | - wrapper.querySelectorAll("[data-calc-var]").forEach(function (el) { |
138 | | - el.addEventListener("input", function () { |
139 | | - saveVarsToLocalByName("{{ $wrapperId }}"); |
140 | | - calc_("{{ $wrapperId }}"); |
141 | | - updateVariableValueSpans(); |
142 | | - }); |
143 | | - el.addEventListener("change", function () { |
144 | | - saveVarsToLocalByName("{{ $wrapperId }}"); |
145 | | - calc_("{{ $wrapperId }}"); |
146 | | - updateVariableValueSpans(); |
147 | | - }); |
148 | | - }); |
149 | | - } |
150 | | - updateVariableValueSpans(); |
151 | | -}); |
152 | | - |
153 | | -// Function to update all variable-value spans |
154 | | -function updateVariableValueSpans() { |
155 | | - document.querySelectorAll('.qna-variable-value').forEach(function(el) { |
156 | | - var name = el.getAttribute('data-var-name'); |
157 | | - var val = localStorage.getItem('qna_var_' + name); |
158 | | - el.textContent = val !== null ? val : ''; |
159 | | - }); |
160 | | -} |
161 | | - |
162 | | -function restoreVarsFromLocal(wrapperId) { |
163 | | - var wrapper = document.getElementById(wrapperId); |
164 | | - if (!wrapper) return; |
165 | | - wrapper.querySelectorAll("[data-calc-var]").forEach(function (el) { |
166 | | - var name = el.getAttribute("data-calc-var"); |
167 | | - var stored = localStorage.getItem(getVarKey(wrapperId, name)); |
168 | | - if (stored !== null) { |
169 | | - if (el.value !== undefined) { |
170 | | - el.value = stored; |
171 | | - } else { |
172 | | - el.textContent = stored; |
173 | | - } |
174 | | - } |
175 | | - }); |
176 | | -} |
177 | | - |
178 | | -// On DOMContentLoaded, restore values and set up listeners |
179 | | -window.addEventListener("DOMContentLoaded", function () { |
180 | | - restoreVarsFromLocal("{{ $wrapperId }}"); |
181 | | - calc_("{{ $wrapperId }}"); |
182 | | - var wrapper = document.getElementById("{{ $wrapperId }}"); |
183 | | - if (wrapper) { |
184 | | - wrapper.querySelectorAll("[data-calc-var]").forEach(function (el) { |
185 | | - el.addEventListener("input", function () { |
186 | | - saveVarsToLocal("{{ $wrapperId }}"); |
187 | | - calc_("{{ $wrapperId }}"); |
188 | | - }); |
189 | | - el.addEventListener("change", function () { |
190 | | - saveVarsToLocal("{{ $wrapperId }}"); |
191 | | - calc_("{{ $wrapperId }}"); |
192 | | - }); |
193 | | - }); |
194 | | - } |
195 | | -}); |
196 | 107 |
|
197 | 108 | </script> |
198 | 109 |
|
|
0 commit comments