KoolKabin
KoolKabin

Reputation: 17653

How do i change form's action property when button is clicked

I would like to change the form's action property when a button is clicked

I have two buttons in a form and when each button is clicked each should post data to own url.

html:

<form method="post">
<input type="button" value="Edit Selected..." />
<input type="button" value="Delete Selected..." />
...
</form>

Upvotes: 1

Views: 6512

Answers (2)

KoolKabin
KoolKabin

Reputation: 17653

GOD it was so similar to native javascript that i missed it...

i am not sure its plane javascript or with jquery but i got the following code exactly as i needed and worked fine:

        $('#cmdDeleteButton').click(function(){
            this.form.action = "http://newurl.com";
        });

Upvotes: 2

Gowri
Gowri

Reputation: 16835

using jQuery

<form method="post" id="form1">
<input type="button"  data-url="action2"  value="Edit Selected..." />
<input type="button" data-url="action1" value="Delete Selected..." />
...
</form>

script:-

$(document).ready(function(){

    $('input[type*="button"]').click(function(){
    var url = $(this).attr('data-url');


     $('#form1').attr('action',url);   

    });

});

Upvotes: 4

Related Questions