Updating the form action using jQuery when submitting

One of the challenges I am facing involves modifying the action URL of a filter form using jQuery.

<form id="filter_form" action="">
     <div style=" bottom: 0; z-index: 1; width: calc(100% - 20px);">
        <input id="filter_submit" type="submit" value="Suchen" data-theme="b" >
      </div>
</form>


$("#filter_submit").on("click", function(e){
   $("#filter_form").attr("action", "/test/").submit();
});

Unfortunately, this approach is not yielding the desired results. Is there another way to achieve this?

Answer №1

To make it work, simply remember to cancel the original event. For instance, take a look at this demonstration: https://jsfiddle.net/6oqy5jLz/

$('input').on('click', function(e) {
  $('form').attr('action', '/bar').submit();
  return false;
});

Answer №2

Absolutely achievable! You're very close.

All you need to do is make sure the form doesn't submit until you've updated the action.

Give this a shot (not tested - but it should do the trick):

$("#filter_submit").on("click", function(e){
   e.preventDefault();
   $("#filter_form").attr("action", "/test/").submit();
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Check to see if modifying the HTML using jQuery results in any errors

Although it may sound straightforward, let me clarify. I am utilizing ajax calls to update the content of the body and I aim to trigger an alert if the updating process fails on the client side, specifically after receiving a response from ajax in case of ...

Detecting the Escape key when the browser's search bar is open - a step-by-step guide

One feature on my website is an editor window that can be closed using the Escape key. The functionality is implemented in JavaScript: $(document).keyup( function(e) { // Closing editor window with ESCAPE KEY if(e.which == 27) { // Clic ...

The jQuery fadeOut function modifies or erases the window hash

While troubleshooting my website, I discovered the following: /* SOME my-web.com/index/#hash HERE... */ me.slides.eq(me.curID).fadeOut(me.options.fade.interval, me.options.fade.easing, function(){ /* HERE HASH IS CLEARED: my-web.com/index/# * ...

Storing data with jQuery

Looking to store objects in jQuery for events. Each event will have a date, title, and some text that needs to be stored in an array. Wondering if using a multi-dimensional array would be the best way to go so I can easily iterate through them with a count ...

What steps do I need to take in order to transform my standard filter form that uses a submit button, into one

Currently, there is a table of venues displayed on the index page. Users can filter these venues by area and type using a form with a submit button that triggers a page reload. After filtering, all the matched venues are shown, and the form automatically ...

Exploring the applications of the `this` keyword within a jQuery-based JavaScript object

Recently, there has been a challenge in creating a JavaScript object with the specific structure outlined below: function colorDiv(div){ this.div=div; this.div.bind("click",this.changeColor) this.changeColor(){ this.div.css(" ...

the click event fails to trigger when the id or class is modified

Hey there, I'm new to working with jquery and I've encountered a problem. Here's the code snippet: http://jsfiddle.net/8guzD/ $('#test.off').click(function(){ $(this).removeClass('off').addClass('on'); }) ...

Update the text within a list item using jQuery

Looking for help with a simple UL <ul id=ABC> <li>AAABBBCCC</li> <li>BBBBB</li> </ul> I'm seeking a JavaScript function using jQuery. The function should take one argument, so that if "BB" is entered, it will upda ...

The functionality of jQuery's .hide method is not effective in this specific scenario

HTML section <div class="navigation-bar"></div> Jquery & JavaScript section function hideUserDiv(){ $('.ask-user').hide(); } var ask = '<div id="ask-user" style="block;position:absolute;height:auto;bottom:0;top ...

Maintaining an active link in a navigation list: A guide for Twitter Bootstrap

How can I make a link appear active when clicked, and keep it active while the user is on that page? (Using Symfony2 with Twitter bootstrap) <ul class="nav nav-list"> <li class="active"> <a href="/link1">Link</a> </li> &l ...

Unable to add to content that is generated dynamically

Upon loading the page, I noticed that the #navbarUsername field remains empty while all other scripts below it run correctly. Despite attempting to use $(document).on('ready', '#navbarUsername', function () {..., this did not resolve th ...

Having trouble getting $compile to work in an injected cshtml file with Angular

Summary I am currently working on a large application where everything is designed to be very generic for easy expansion. One of the components I am focusing on is the dialog. Before suggesting alternatives like using ngInclude or angular templates, let m ...

How can you apply a class to a different element by hovering over one element?

Is there a way to darken the rest of the page when a user hovers over the menu bar on my website? I've been playing around with jQuery but can't seem to get it right. Any suggestions? I'm looking to add a class '.darken' to #conte ...

When working with ASP.NET MVC 3, be aware that the ContentResult may not accept a class that inherits from

After my previous question regarding finding nearby entities with Bing Maps REST control, I have made some modifications. Within the for loop of the function DisplayResults(response), the following code has been added: var loc = new Array(); loc[0 ...

Tips for transferring the id from the url to a php function seamlessly without causing a page refresh

I have a div that includes a button (Book it). When the button is clicked, I want to append the id of the item I clicked on to the current URL. Then, use that id to display a popup box with the details of the clicked item without refreshing the page, as it ...

Tips for maximizing efficiency and minimizing the use of switch conditions in JavaScript when multiple function calls are needed

After coming up with this code a few minutes ago, I began to ponder if there was a more elegant way to optimize it and condense it into fewer lines. It would be even better if we could find a solution that eliminates the need for the switch condition altog ...

Retrieving information from a Vue component using AJAX

One of the components in my Vue project is named "testrow". It contains a form with two selections: "description" and "parameter". I need to make sure that the "parameter" changes based on the selected 'description' value stored in my database. T ...

JSON is functioning properly, however, I am encountering difficulties trying to make JSONP work

Hello, I have been working on trying to make some JSON work properly. Here is the code snippet that I have written: $.getJSON("/calendar/event/test", function(data) { $.each(data, function(i,item){ $('#test').append('<li>' ...

Calculating the number of items in the Ajax response list

Using jQuery to Count List Items $.ajax({ url : "/assets/inc/list.php", success : function (data) { console.log($(data).filter('li').size()); } }); By implementing the above code, I attempted to calculate the total number of ...

Arrange a div element within another div by utilizing the fixed positioning property, while also accounting for any potential "white space

Can someone help me figure this out: I currently have this much progress: http://jsbin.com/apikiw/3/edit#preview The issue I'm facing is that I am unable to insert it between the <p /> tags due to its dynamic nature... any solutions or suggest ...