Youss
Youss

Reputation: 4212

Change the 'action' of a form

I can submit using Jquery like this:

$('.SomeLink').bind('click', function()
{
      $(form).submit();
});

The actual form looks like this:

<form id="foo" method="get" action="http://www.google.com/search">
</form>

Now suppose I have a another link in which I want to be able to use the same form however now I want to change the url of 'action'. How can I change the action if a different link is clicked?

Upvotes: 2

Views: 228

Answers (5)

Ja͢ck
Ja͢ck

Reputation: 173542

I'm assuming you have a form with a textbox and several links that can submit the form to a location of choice. This is how you can make it generic:

<form id="theform">
    <input type="text" name="q" />
</form>
<div id="choices">
    <a href="#" data-url="http://www.google.com/search/">Google</a> 
    <a href="#" data-url="http://www.yahoo.com/">Yahoo</a>
</div>

Then the JavaScript:

$(function() {
  $('a', $('#choices')).on('click', function() {
    $('#theform')
        .attr('action', $(this).data('url'))
        .submit();
    return false;
  });
});

Upvotes: 1

Naveed
Naveed

Reputation: 42093

Try:

$('.DifferentLink').bind('click', function() {
      $("#foo").attr("action","new/action");
      $("#foo").submit();
});

Upvotes: 1

Emaborsa
Emaborsa

Reputation: 2830

You could try to edit action as a simple parameter:

$(form).attr('action', 'newUrlInString');

and then call submit();

Upvotes: 2

Thinking Sites
Thinking Sites

Reputation: 3542

The action is just an attribute of the form.

$("different button").click(function(){
     $("form").attr("action","new action");
});

Upvotes: 3

Sagiv Ofek
Sagiv Ofek

Reputation: 25270

$("#foo").attr("action","http://your_new_val");

Upvotes: 4

Related Questions