1

Is it possible to retrieve the card element object by referencing the ID of the DOM element that it is attached to, with the Stripe elements API in Javascript? For example, if I create the card element like this:

var card = elements.create('card', {style: style});
card.mount('#card-element');
...
...
var card = elements.retrieve(card-element') // in some other function

...or do I have to store the card object in a variable? The reason I'm asking is because I would like to clear the credit card details entered in the form at a later stage, and I was hoping there was a way to retrieve the card object without actually storing a reference to it in memory.

RTF
  • 6,214
  • 12
  • 64
  • 132
  • in second parameter of create you have not added attribute id – Abhishek Shukla Apr 02 '18 at 12:42
  • @AbhishekShukla I don't understand. The docs don't mention setting ID attribute in that function: https://stripe.com/docs/stripe-js/reference#elements-create - And anyway, how does that help me retrieve the object after creating it? – RTF Apr 02 '18 at 12:48
  • What are you trying to do with the card element? `#card-element` is just a placeholder `
    ` or similar in your layout. The actual Elements form is hosted in an iframe created by Stripe and you don't have access to the internals.
    – duck Apr 02 '18 at 16:10
  • @duck I just want to get hold of the card object so I can clear the form. Like I was saying, I know I can store a reference to it in memory, but I was just hoping there's a way to retrieve it dynamically e.g. `var card = elements.retrieve('card-element')` without storing the reference – RTF Apr 02 '18 at 16:12
  • What you should be able to do is `card.clear()` --- see https://stripe.com/docs/stripe-js/reference#other-methods – duck Apr 02 '18 at 16:14
  • @duck I know that, but I need a reference to the card object first! – RTF Apr 02 '18 at 16:14
  • There's no way to retrieve the card element, you'd use `card` above. Example that clears the field, hit the 'clear' button https://jsfiddle.net/3pyr9r3e/1/ – duck Apr 02 '18 at 16:20

0 Answers0