export function AssessScore_click(event,$w) {
$w(“#benefitscore”).text = $w(“#dropdown10”).value + $w(“#dropdown9”).value;
$w(“#benefitscore”).show();
}
the things that the code do is concatenate rather add them together.
let say dropdown 10 = 1; dropdown9 = 2
actual result is 12, but expecting 3
can any experts help?
Sounds and looks like you’re concatenating two (2) strings when what you actually want to do is add two (2) numbers; in lieu of the following:
$w("#benefitscore").text = $w("#dropdown10").value + $w("#dropdown9").value;
try:
$w("#benefitscore").text = Number($w("#dropdown10").value) + Number($w("#dropdown9").value);
or:
$w("#benefitscore").text = parseInt($w("#dropdown10").value) + parseInt($w("#dropdown9").value);
(In addition, I recommend that you replace “dropdown10” and “dropdown9” with more user-friendly, mnemonic names just like you already did with “benefitscore”.)
thanks abergquist
i tried.
the error:
Wix code SDK error: The text parameter that is passed to the text method cannot be set to the value 4. It must be of type string.
Looks like you then need to convert the sum back to a string; here a couple of solutions:
var benefitsScore = Number($w("#dropdown10").value) + Number($w("#dropdown9").value)
$w("#benefitscore").text = benefitsScore.toString();
or:
var benefitsScore = parseInt($w("#dropdown10").value) + parseInt($w("#dropdown9").value)
$w("#benefitscore").text = benefitsScore.toString();
(In addition, I recommend that you replace “dropdown10” and “dropdown9” with more user-friendly, mnemonic names just like you already did with “benefitscore”.)