1

I have a form that contains an input field for an email address. The form doesn't have a submit button. Instead it has the Stripe checkout.js script which provides a button that triggers a roundtrip to Stripe (to process a credit card) before submitting the form. The checkout.js script allows an optional variable data-email which makes it possible to pass a preset email address to the Stripe checkout form. I'd like to set the data-email variable with the value of the email address input field on my own form.

Here's the form and the script:

<form role="form" class="new_user" id="new_user" action="/users" method="post">
    <label for="user_email">Email</label>
    <input class="form-control" type="email" value="" name="user[email]" id="user_email" />
   <script src="https://checkout.stripe.com/v2/checkout.js"
       class="stripe-button"
       data-email=document.getElementById('user_email').value
       data-key="stripe_key"
       data-description="Product"
       data-amount="500">
   </script>
</form>

I know I need to use:

data-email=document.getElementById('user_email').value

But the data-email isn't getting set. Do I need to add an onchange property to the input field? What would that look like? Do I need more than that?

1

1 Answer 1

2

If the user is entering his email on the page, you might as well just update the script as they type:

var stripe = document.getElementById("stripe");
document.getElementById("email").onkeypress = function () {
    stripe.setAttribute("data-email", this.value);
}
<input id="email">
<script id="stripe"></script>

If you run the snippet and inspect the input element, then type, you'll see the data-email attribute update for the given stripe script tag. You should be able to adapt this to your form.

5
  • Could you show me a JSFiddle that works with the code provided? I can see that your code modifies the data-email attribute in the script. But it appears that once the script is loaded, though the attribute can be changed, the original value is still used and the changes are ignored. Commented Nov 14, 2014 at 22:03
  • @DanielKehoe I've been trying to make one, but I don't have an API key (I'm not sure whether that's affecting how the email is passed). Either way, you don't have to pass an email to stripe. It's an optional field, meaning the user can just type it in when the stripe modal loads.
    – royhowie
    Commented Nov 15, 2014 at 19:42
  • Thanks @royhowie, I don't think you need an API key to test the code. I know the email data attribute is optional. I'm creating a sign up form (Rails Devise) where the user enters an email address (and password) and then clicks "pay with card" and I don't want the user to enter the email address twice so I'm trying to update the email data attribute in the script from the input field on sign up form. However, at this point I don't believe it is possible because the email data attribute is set as soon as the script is loaded and a change to the script is ignored. Commented Nov 15, 2014 at 20:35
  • @DanielKehoe if the user is logging in, you could dynamically insert the script element with the correct info, once their info is confirmed by the server.
    – royhowie
    Commented Nov 15, 2014 at 20:37
  • Thanks again for the thoughts. In this use case, the user is making a payment and creating a new user account at the same time. I've implemented a two-step process where the user fills out the sign up form, the page refreshes and sets the email data attribute (as you suggested), then the user clicks "pay with card." I was hoping to make it one step with JavaScript changing the variable but it seems that doesn't work since the script runs on page load. Commented Nov 15, 2014 at 20:49

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.