user2958509
user2958509

Reputation: 43

OnClick pass url parameter

This is my very first question here after looking for many hours.

I have a button:

<a class="btn" onclick="return filter('http://mysite/filter');">Filter</a>

That calls this little script:

<script language="javascript"> 

function filter(page)
{
    //alert('proceed filter!!');
    document.form.action = page;
    document.form.submit();
    return true;
}

</script>

Adapted from: http://www.codeproject.com/Articles/664/Specifying-multiple-actions-from-a-single-Form

It all works actually, but the original post is from 2000 (13 years old)

So I was wondering if I should maybe update this code using jQuery.

I have the button with the URI I what to pass and submit.

I just don't know how to transform the javascript above to jQuery, if its at all necessary.

Any suggestions are much appreciated.

Upvotes: 2

Views: 9010

Answers (1)

Mingle
Mingle

Reputation: 836

You can do something like this:

<a class="btn filter-btn" data-url="http://mysite/filter">Filter</a>

$('.filter-btn').on('click', function() {
  $('form').prop('action', $(this).attr('data-url')).submit(); // I recommend giving your form an ID or something instead of just matching on the form element.
});

With this approach you can have multiple a.filter-btns each with their own data-url and the code to process it lives in one place and not in many different onclicks.

Upvotes: 1

Related Questions