I am relatively new to coding.
I’m making a character sheet using html, css, and javascript. Within this character sheet, I am using javascript to update certain parts based on input.
Right now, I have an input number for level, which once an input takes place, it spits out a resulting number called Combat Mastery, which is this code below.
html code:
<div class="content-row">
<label class="field-title">Combat Mastery</label>
<p id="cm" type="number" class="field-data"></p>
</div>
Here is the java function that finds the Combat Mastery value:
function combatMastery() {
// get the input element and it's value
let inputElement = document.getElementById('c-level');
let inputVal = inputElement.value;
//lets get the calculation to divide by 2 and round up
let cm = Math.ceil(inputVal/2);
console.log("CM Value:", cm);
//DISPLAY IT
let result = document.getElementById('cm');
result.textContent = cm;
}
This works perfectly fine for my uses (at this point and time).
I have 4 “abilities” that all have a numeric value, based on a number input.
I have a separate area called “saves” which players can choose to ‘specialize’ in. When they ‘specialize’ in it, they click a checkbox, which is supposed to add the ‘ability’ score and the ‘combat mastery’ score together, and output a result.
Below is one example of a WORKING script that correctly functions as I expect it to. I’ve debugged this and find that the chaValue and cmValue (which might get added together) are both NUMBERS.
function chaSave() {
let checkbox = document.getElementById('cb-save-cha');
let chaValue = parseInt(document.getElementById('cha').value);
let cmValue = parseInt(document.getElementById('cm').textContent);
let saveChaElement = document.getElementById('save-cha');
if (checkbox.checked){
let chaSaveResult = chaValue + cmValue;
saveChaElement.textContent = chaSaveResult;
}
else {
saveChaElement.textContent = chaValue;
}
console.log("CHAcmValue:",cmValue);
console.log("CHA value:",chaValue);
};
For 3 of the 4 “abilities” the “save functions” above work, except for the last one.
Here’s the java for the intelligence save that shows NaN when the checkbox is checked:
function intSave() {
let checkbox = document.getElementById('cb-save-int');
let intValue = parseInt(document.getElementById('int').value);
let cmValue = parseInt(document.getElementById('cm').textContent);
let saveIntElement = document.getElementById('save-int');
if (checkbox.checked){
let intSaveResult = intValue + cmValue;
saveIntElement.textContent = intSaveResult;
}
else {
saveIntElement.textContent = intValue;
}
console.log("INTcmValue:",cmValue);
console.log("INT value:",intValue);
};
the INTcmValue returns NaN in the console, but I am so lost as to why that is?
The html as a reference for both the Cha save and the Int save is here:
<div class="content-row">
<input id="cb-save-cha" type="checkbox" oninput="handleChaInput()">
<label class="field-title" data-dice-type="1d20" data-modifier="no-mod" data-label="Save Charisma">Charisma</label>
<p id="save-cha" type="number" class="field-data-short"></p>
</div>
<div class="content-row">
<input id="cb-save-int" type="checkbox" oninput"handleIntInput()">
<label class="field-title" data-dice-type="1d20" data-modifier="no-mod" data-label="Save Intelligence">Intellect</label>
<p id="save-int" type="number" class="field-data-short"></p>
</div>
To me, the cmValue within the chaSave function and the intSave function are pulling from the same place. I’ve even copied the cmValue line from the chaSave and pasted it directly into the intSave function, and the intSave operates such that when the checkbox for that save is checked, it still returns NaN.
I’m confused because in all other instances (the other 3 abilities) the save function works perfectly… why is it returning NaN when it’s IDENTICAL to the others? What am I missing?
Please keep in mind I am very new to java and I need to be explained like I’m 5 what’s happening.
Please see above. I tried duplicating the code, and have gone over the relevant html areas and java code and to me it appears identical, it’s just spitting out NaN for an unknown reason when it works everywhere else.