-2

AMP-SCRIPT gurus!

I used a <amp-bind-macro> tag (from an example) to multiply the value of an <input type="number"> field by a constant value. It worked! Now, I'm trying to multiply different values from different INPUTs and I cannot:

  • find a way to reference those values.
  • pass those values to the (ehm...) function.
  • enjoy my first hours playing with AMP-SCRIPT.

Simply put...

I need to assign the result of this operation to a <span>

<span> = (12 + (inputA * 8) + (inputB * 2) + (inputC * 5))

Before you ROFLOL, that is not a code at all. I was just trying to picture the problem.

2 Answers2

0

Guess what? I found how. Basically, you need to:

  • initialize variables with values.
  • add the macro (like the function, its parameters and the math operations.
  • tell each INPUT to update its corresponding variable.
  • assign the result to the SPAN.
0

For inputs, you need bind the value via AMP.setState()

Then in <amp-bind-macro /> expression, just call those variables, don't need arguments here.

Here's example for sum 2 input:

<amp-bind-macro id="sum" expression="numberA + numberB">

<input type="number" value="0" on="input-throttled:AMP.setState({ numberA: event.value })">
<input type="number" value="0" on="input-throttled:AMP.setState({ numberB: event.value })">

<div>
  Result <span [text]="sum()">0</span>.
</div>
Huy Nguyen
  • 2,025
  • 3
  • 25
  • 37