1

I have a submit on my page:

<input type=​"submit" name=​"nextstep" value=​"Next" id=​"nextstep" class=​"newaddressnextstepbutton">​

and when I click on this submit I need to make another submit on first button with specific name like this:

<input type="button" name="selectBillingAddress" value="First one" class="selectbillingaddressbutton" onclick="setLocation('/checkout/selectbillingaddress?addressId=537')">
<input type="button" name="selectBillingAddress" value="Second one" class="selectbillingaddressbutton" onclick="setLocation('/checkout/selectbillingaddress?addressId=647')">

I did it like this:

$("input[name='nextstep']").click(function() {
        $("input[name='selectBillingAddress']")[0].submit();
});

But this not working. I get this error: Uncaught TypeError: $(...)[0].submit is not a function I found this answer for this error "Submit is not a function" error in JavaScript but I dont have input[name='submit'] on this page..

Can anybody help me?

Manoj Kadolkar
  • 717
  • 8
  • 22
Kicker
  • 606
  • 1
  • 12
  • 27

4 Answers4

1

You need to call the submit function on the form, not the button. So it should be something like this:

  $("input[name='nextstep']").click(function() {
       $("form").submit();
  });

If you have more than one form on your page give your form an ID and use that instead.

MihaiB
  • 44
  • 4
1

.submit() is only available on form elements.

Instead you need to trigger a native click event on the first matching DOM element instead:

e.g.

$("#nextstep").click(function(e) {
     e.preventDefault();
     $("[name='selectBillingAddress']")[0].click();
});

Notes:

  • You should target the element using an id selector is available (faster/shorter)
  • As a good practice, you should prevent the original click event from submitting the form, and not just assume the redirect will override it.
  • You need to use .click() on the DOM element to call the native click handler.
iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202
0

Sorry: Here is the UPDATED one.

$("input[name='nextstep']").click(function() {
        $("input[name='selectBillingAddress']").eq(0).trigger("click");
});
Rustin Cohle
  • 161
  • 1
  • 1
  • 12
-1

Try to invoke the natural click of the button,

$("input[name='nextstep']").click(function() {
     $("input[name='selectBillingAddress']")[0].click();
});

Or just grab the closest form and invoke .submit() over it. Since elements other than form will not have submit function as its member.

Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130