guest
guest

Reputation: 2224

What does $.get(0) mean?

I saw it on the stripe code, but i haven't used it this way on a form submission.

https://gist.github.com/briancollins/6365455

What is the reason for using .get(0)?

var stripeResponseHandler = function(status, response) {
  var $form = $('#payment-form');

  if (response.error) {
    // Show the errors on the form
    $form.find('.payment-errors').text(response.error.message);
    $form.find('button').prop('disabled', false);
  } else {
    // token contains id, last4, and card type
    var token = response.id;
    // Insert the token into the form so it gets submitted to the server
    $form.append($('<input type="hidden" name="stripeToken" />').val(token));
    // and re-submit
    $form.get(0).submit();
  }

Upvotes: 0

Views: 4512

Answers (2)

Karl-Andr&#233; Gagnon
Karl-Andr&#233; Gagnon

Reputation: 33870

As said in the comment :

Check the docs: api.jquery.com/get $form.get(0) is the same as $form[0]. It get the DOM element at position 0 out of the jQuery object. – Rocket Hazmat

In this context it use used to submit the form.

You see, when you use .submit() on a jQuery Element, you trigger the jQuery method .submit() which is the same as .trigger('submit') (ref. here). Using get(0).submit() here will the submit method of HTMLDomElement, which is the native form submit.

Why would you use the native submit instead of the jQuery submit? Simply because the native submit doesnt trigger .on('submit') or other event bind on submit before actually submitting the form.

Upvotes: 3

jlblatt
jlblatt

Reputation: 266

It gets the first matching DOM element from the jQuery object

http://api.jquery.com/get/

Upvotes: 0

Related Questions