inherit
114253
0
Sept 23, 2024 7:58:54 GMT -8
Teg
Can't Wait for V6
3,157
November 2007
teg
|
Post by Teg on Sept 5, 2023 12:40:38 GMT -8
So, I want to take 2 numbers represented by $[total_members_online] and $[total_staff_online] and add them together and then place that sum in a span tag. This is how I'm trying to do it, and it's not working. I think I've been looking at code too damn long today and missing stupid stuff. LOL Please help:(
<a href="https://hwsoww.boards.net/members" title="Marauder's Map">Marauder's Map</a>( <script> function addTotalMOnline() { var num1, num2, sum; num1 = parseInt(document.getElementById("mO").value); num2 = parseInt(document.getElementById("sO").value); sum = num1 + num2; document.getElementById("combinedTotalMembersOnine").value = sum; </script> <span style="visibility:hidden;" id="mO">$[total_members_online]</span> <span style="visibility:hidden;" id="sO">$[total_staff_online]</span> <span id="combinedTotalMembersOnline"></span>)
|
|
inherit
Official Code Helper
65613
0
1
Oct 22, 2024 1:56:19 GMT -8
Chris
"'Oops' is the sound we make when we improve"
9,017
December 2005
horace
RedBassett's Mini-Profile
|
Post by Chris on Sept 5, 2023 23:49:39 GMT -8
So, I want to take 2 numbers represented by $[total_members_online] and $[total_staff_online] and add them together and then place that sum in a span tag. This is how I'm trying to do it, and it's not working. I think I've been looking at code too damn long today and missing stupid stuff. LOL Please help:( <a href="https://hwsoww.boards.net/members" title="Marauder's Map">Marauder's Map</a>( <script> function addTotalMOnline() { var num1, num2, sum; num1 = parseInt(document.getElementById("mO").value); num2 = parseInt(document.getElementById("sO").value); sum = num1 + num2; document.getElementById("combinedTotalMembersOnine").value = sum; </script> <span style="visibility:hidden;" id="mO">$[total_members_online]</span> <span style="visibility:hidden;" id="sO">$[total_staff_online]</span> <span id="combinedTotalMembersOnline"></span>) I can identify a few issues just from eyeballing the code: - There's a function defined but the function is never called
- The function is missing its closing curly brackets
- The #mo and #so spans are not form fields so their text values would be accessed via textContent or the legacy innerText (you could also use innerHTML but that would unnecessarily introduce exploit opportunities)
You could move the script below the created spans and then have the function invoke itself via an IIFE. You could also leave the script where it is and add a second script below the spans that then calls the function defined in the first script. In any case, the function would be useless if it runs before the elements that are being referenced have yet to be created, which is why we would move its invocation below the spans (either physically or through some delayed execution mechanism such as document ready) to ensure they have been created in the DOM before the function runs. If the #m0 and #s0 spans are to remain hidden and serve no other purpose then they can be eliminated and the variables inserted directly into the script itself <span id="combinedTotalMembersOnline"></span> <script> document.getElementById('combinedTotalMembersOnine').textContent = parseInt($[total_staff_online],10) + parseInt($[total_members_online],10); </script>
(assumes total_staff_online and total_members_online have no commas)
|
|