javascript - Change form action based on submit button -


i have many forms following on page. want change form action based on submit button user clicked (and of course submit form)

<form action="/shop/products.php" data-ajax="false" method="post">     <div data-role="fieldcontain">          <input name="submit" class="obutn" type="submit" value="order" />         <input name="submit" class="oebutn" type="submit" value="extras" />      </div> </form> 

i tried with

$(".obtn").click(function() {     $(this).parent().parent().attr("action", "/shop/products.php");      }); $(".oebtn").click(function() {     $(this).parent().parent().attr("action", "/shop/extras.php");        }); 

but form submited products.php. can tell me what's wrong?

there 2 typos:

  • obtn instead of obutn
  • oebtn instead of oebutn

another suggestion use closest("form") form contains clicked button.

$(".obutn").click(function() {     $(this).closest("form").attr("action", "/shop/products.php");      }); $(".oebutn").click(function() {     $(this).closest("form").attr("action", "/shop/extras.php");        });  $("form").on("submit", function () {     alert($(this).attr("action")); }); 

jsfiddle


Comments