Muhammad Arsalan Toor
Muhammad Arsalan Toor

Reputation: 201

How do we pass dynamic variable into JavaScript dictionary

i have mentioned bellow code i want that when user put amount in input field i want this amount to be add in java script amount key

form

  <form id="payment-form">
      <input type="number" name="amount" id="amount">
  </form>

script

 <script>
 SpreedlyExpress.init("C7cRfNJGODKh4Iu5Ox3PToKjniY", {
 "amount": "$9.83",
 "company_name": "AA crypto hedge"
 });
 </script>

actually i want to change amount dynamicaly in $9.00 when user enter amount in input field

Upvotes: 0

Views: 154

Answers (1)

Pavlos Karalis
Pavlos Karalis

Reputation: 2976

You'd attach a submit handler to the form and call your function inside, passing a variable for the amount:

<form id="payment-form" onsubmit="handleSubmit(event)">
  <input type="number" name="amount" id="amount">
</form>

<script>
const amount = document.getElementById("amount");

const mockSpreedlyExpress = {
  init: (str, obj) => console.log(str, JSON.stringify(obj))
}

function handleSubmit(e) {
  e.preventDefault();
  if(!amount.value) return;
  mockSpreedlyExpress.init("C7cRfNJGODKh4Iu5Ox3PToKjniY", {
    amount: amount.value,
    company_name: "AA crypto hedge"
  });
  amount.value = "";
}
 
</script>

Upvotes: 1

Related Questions