Gauging by other's questions on this forum, I'm rather embarrased to ask this simple question...
I have a Dynamic Page that loads in my Collection information great. Once the form is ready, I just need to perform a simple price markup calculation. I have two text elements on my form, Supplier Price and Customer Price.
When the Form is ready with the Collection data, is there a way to get the Customer Price text element to display the calculated markup price?
Example calculation: Supplier Price * .25 + Supplier Price
It seems I'm having problems parsing the text to a number to perform the calcution.
The following code:
$w.onReady( () => {
$w("#dynamicDataset").onReady( () => { // Allow dataset to load
var supPrice = String(parseInt($w("#text32")).value);
var cusPrice = supPrice * .25 + supPrice;
$w("#text37").text = cusPrice;
console.log("Customer Cost: " + cusPrice);
My result for the "console.log" is:
Customer Cost: NaNundefined
Glad I could help. Gives me the warm fuzzies.
Thank you Yisrael. I've got to pay more attention to those little details!
You have the closing parenthesis in the wrong place.
Not this:
var supPrice = Number($w("#text32")).text;
But this:
var supPrice = Number($w("#text32").text);
By way of explanation, let's write the code the long way:
var textValue = $w("#text32").text; var supPrice = Number(textValue);
As you can see, it's just a matter of parenthesis placement. Javascript is a bit fussy like that.
Yisrael
Thanks for the input Yisrael!
I tried both of your suggestions, they both corrected one issue. I no longer receive an "undefined" issue, but I'm still getting a "NaN" error with the following code.
var supPrice = Number($w("#text32")).text;
var cusPrice = supPrice * .25 + supPrice;
console.log("Customer Cost: " + cusPrice);
In all the examples I've seen, either the parseInt or Number methods should work, but for some reason mine isn't. :-( Could there be something wrong with the way I have the code ordered?
Hi,
In order to calculate, you need the text as a number. Therefore, you want something like this:
var supPrice = parseInt($w("#text32").text); var cusPrice = supPrice * 0.25 + supPrice;
You can also use Number($w("#text32").text) instead of parseInt($w("#text32").text)
Good luck,
Yisrael