`I’m trying to calculate the cost of a property deed change based on the reason for the change and the user-inputted loan amount. With the code so far, I’m able to calculate and send an alert for reasons 3 and 4, but the other reasons aren’t outputting the appropriate alert/cost. Some of the code is probably redundant, but this is one of my first projects so please cut me some slack.
HTML
<select id="menu" required>
<option value="r0">Select</option>
<option value="r1" id="reason" class="reason">1. Corrective Deed</option>
<option value="r2" id="reason" class="reason">2. Deed Pursuant to a Dissolution</option>
<option value="r3" id="reason" class="reason">3. Deed to Remove Spouse from Primary Residence Without Dissolution</option>
<option value="r4" id="reason" class="reason">4. Deed to Add a Co-borrower Who is Not Currently on Title. The Co-borrower has to be on the loan and this has to be a Primary Residence.</option>
<option value="r5" id="reason" class="reason">5. Deed to Add Spouse Even if They Are Not on the Loan</option>
<option value="r6" id="reason" class="reason">6. Deed to Remove or Add an Owner on Investment Property or 2nd Home (Only if Requirement of Our Loan)</option>
<option value="r7" id="reason" class="reason">7. Deeds to Take Property Out of a Trust</option>
<option value="r8" id="reason" class="reason">8. Deeds to Take Property Out of a Life Estate</option>
<option value="r9" id="reason" class="reason">9. Deeds Where We are Transferring Full Ownership to Our Borrowers*</option>
</select>
JavaScript
function calculateCost() {
let r = document.getElementsByClassName("reason").value;
let a = document.getElementById("amount").value;
if (r === 'r3' || 'r4') {
cost34 = ((a / 2) * 0.0070);
} else if (r === 'r5') {
cost = 0.0070;
} else if (r === 'r6' || 'r8' || 'r9') {
cost = (a * 0.0070);
} else {
alert("N/A");
}
alert("Total Cost of Deed Change: $" + cost);
};
2
Answers
There are a few things to point out here.
First, ID’s are a unique identifier and cannot be attributed to more than one element, so you should not have
id="reason"
on each of your options.Second, to get the selected option of the select, you want to look at the value of the select and not of the option(s). Ex:
let r = document.getElementById("menu").value;
This will result in r = ‘r1’ if the first option is selected, for example. For more on using select’s with JavaScript, see this question and its answers.Third, writing
if (r === 'r3 || 'r4')
does not work to evaluate whether r is ‘r3’ or ‘r4’. You need to include ther ===
on each value you want to check for, so:if (r === 'r3' || r === 'r4')
would be the correct way to write this. See here for more.Edit: Forgot to mention this, but using
let
to define variables creates "block-scoped" variables, which can only be used within that block. That is why I moved thelet cost
outside of the if blocks. Would probably be worth looking into the differences between var, let, and const for defining variables.Here is a working example:
As a corollary to Ben’s great answer a couple of observations:
A
select
may not be the best choice for input. The length of the text that you’re labelling the options with is really too much for the select box to handle well on any screen let alone a mobile device (for example).I would suggest moving to radio buttons. This would allow you to format the text for each option appropriately.
Since you’re dealing with currency you may want to consider using Intl.NumberFormat to format your output. You can specify the locale, and the type of number you want it formatted to, and the browser will handle the output.