-1

What I want to do is to set PayPal button's "value" -attribute to my variable which I read from a JavaScript file.

<input type="hidden" name="custom" value="VARIABLE HERE">

It didn't work when I tried to add code like this:

<input type="hidden" name="custom" value="<script>code here</script>">

Here is something that I've tried:

<script type="text/javascript">
    function getPaypalPrice() {
        var finalPrice = getUrlVar()["totalprice"] * basePrice;
        return finalPrice;
    }
</script>

And then for the paypal button

<input type="hidden" name="amount" id="paypalPrice" value="   <script>document.getElementById('paypalPrice').value = getPaypalPrice();</script>">

That did not work even when someone suggested that in a previous topic. Is there any other solutions?

Timppa
  • 353
  • 2
  • 7
  • 24
  • Please give a link to the question or answer that told you to put a script inside the double quotes of the value of an html element. – takendarkk Mar 28 '17 at 14:20
  • Very strange; I cannot find the topic anywhere... Not even from my history. It used – Timppa Mar 28 '17 at 14:38
  • http://stackoverflow.com/questions/7764154/pass-a-javascript-variable-value-into-input-type-hidden-value?rq=1 this solution is not working for me either – Timppa Mar 28 '17 at 17:40
  • Show us your code for that. – takendarkk Mar 28 '17 at 17:40
  • edited that to my original post – Timppa Mar 28 '17 at 18:11
  • What you have posted does not match the answers in the question that you linked to. If you write your code like in those answers you shouldn't have a problem. – takendarkk Mar 28 '17 at 18:17
  • Yes, I tried another solution as well before but it didn't work (without – Timppa Mar 28 '17 at 19:11

1 Answers1

1

Don't put any JavaScript or script tags in the <input>. This means in your HTML you have:

<input type="hidden" name="amount" id="paypalPrice" value="0">

Then in your JavaScript (either in a <script> tag or separate JS file) you can reference that input from its ID. Also note that the JavaScript should be loaded at the bottom of the page, so that the #paypalPrice element is already loaded into the browser before the JavaScript code runs.

function getPaypalPrice() {
  console.log(getUrlVar().totalprice);  // check this has a value?
  return finalPrice = getUrlVar().totalprice * basePrice;
}

var paypalPriceElement = document.getElementById('paypalPrice');
paypalPriceElement.value = getPaypalPrice();

As I commented, please check your getUrlVar()['totalprice'] is actually returning a value, since it could be asynchronous and causing you other issues.

Liam Gray
  • 1,089
  • 9
  • 16