Modify the form's action attribute when submitted

I am trying to create a form with multiple buttons that will change the action and target properties when a specific button is clicked.

Below is my code snippet:

<div id="root">
    <form :action="form.action" ref="form" :target="form.target">
        <button >Submit to /test</button>
        <button id="new" @click.prevent="submit">Submit to /new</button>
    </form>
</div>

<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

<script>
    var app = new Vue({
        el: '#root', 
        data: {
            form: {
                action: '/test',
                target: '_blank'
            }
        },
        methods: { 
            submit: function() {
                this.form = {
                    action: '/new',
                    target: '_self'
                };
                console.log(this.$refs.form);
                this.$refs.form.submit();
            }
        }
    })
</script>

After testing, I noticed that the form always submits to /test, even after clicking on the #new button. Additionally, the console.log(this.$refs.form); statement returns

<form action="/new" target="_self">

This brings up the question of why the form is not being submitted to /new as intended. Any ideas?

Answer №1

Apparently, the issue was caused by the prevent function.

This solution is effective:

<div id="root">
    <form :action="form.action" ref="form" :target="form.target">
        <button >Submit to /test</button>
        <button id="new" @click="submit">Submit to /new</button>
    </form>
</div>

<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

<script>
    var app = new Vue({
        el: '#root', 
        data: {
            form: {
                action: '/test',
                target: '_blank'
            }
        },
        methods: { 
            submit: function() {
                this.form = {
                    action: '/new',
                    target: '_self'
                };
            }
        }
    })
</script>

However, I'm unsure why the previous version failed to work.

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

``Why Laravel and Vue JS Routing is displaying a "Page not found" error message

I recently used a tutorial I found at However, when trying to access localhost/admin/companies, I encountered a "Page Not Found" error. I attempted to make changes as follows: const router = new VueRouter({ routes: [ { path: '/admin/companies&a ...

Display the div if the input field is left blank

Is there a way to display the div element with id="showDiv" only if the input field with id="textfield" is empty? <form action=""> <fieldset> <input type="text" id="textfield" value=""> </fieldset> </form> <div id="sh ...

When attempting to click on the dropdown in Bootstrap, there is no content displayed

I am currently practicing Bootstrap and focusing on implementing Dropdowns. However, I am facing an issue where upon clicking the Dropdown button, nothing appears on the screen. Preview when it's not clicked Preview when it's clicked Here is m ...

Issues have arisen with the functionality of noneditable contents in tinymce

I am currently using the tinyMCE editor and I need to make certain content readonly (nonEditable). According to the documentation, if I apply the class "mceNonEditable" to specific elements, it should meet this requirement. However, when I select that ele ...

React-querybuilder experiencing issues with validator functionality

While utilizing the react-querybuilder, I have encountered an issue with field validation not functioning correctly. Upon reviewing this StackBlitz, it appears that when clicking on Rule and checking all fields, there are no errors present. export const fi ...

The issue of Next.js redux useSelector causing HTML inconsistency

Currently, I am utilizing Next.js for the development of a React application. In order to manage the state, I have integrated redux along with redux-toolkit. A peculiar error has surfaced in the console with the message: Warning: Did not expect server H ...

Most effective method for integrating a JS module across all browsers

I have created a robust library that I want to integrate into a different web project. This library handles all its dependencies and consists of js, css, and html files. My ideal scenario would be to package it as an npm module and simply use import to in ...

What steps should be taken in PHP to display a popup when the user input is found to be invalid?

I've been working on implementing a popup in PHP to show if the user enters an existing username, as per our teacher's requirement to use popUp instead of alert. I have set the visibility property of the popup to "Hidden" in CSS; <div class=&q ...

Guidelines for submitting a form using ajax in Vue version 2.6

Being new to Vue, I am exploring different ways to submit a form. Below is the simple form that I have created: <form :action=" appUrl +'ConnectionHandler'" method="post" enctype="multipart/form-data"> <fieldset id="fileHandlin ...

Encountered a problem while trying to deploy my application on Heroku

Hey everyone, I'm facing a tricky error even though I followed all the correct steps. My deployment keeps failing and I can't figure out why. I really need to get my app deployed for a demo, so any help would be greatly appreciated. I've eve ...

Increase the time of a Date by 10 seconds

Is there a way to increase the time of a JavaScript date object by 10 seconds? For example: var currentTime = new Date(); var currentSeconds = currentTime.getSeconds() + 10; currentTime.setSeconds(currentTime.getSeconds() + currentSeconds); ...

injecting javascript dynamically using jquery

I am attempting to conditionally load a script in the case that the browser being used is IE8. To achieve this, I have employed jQuery's .getScript function as it allows me to execute certain actions after the script has been loaded. The issue lies in ...

Exploring prototypal inheritance through Angularjs module.service设计(Note: This

Having worked with Angularjs for a few months now, I am curious about implementing efficient OOP within this framework. Currently, I am involved in a project where I need to create instances of a "terminal" class that includes constructor properties and v ...

Tips for properly updating node.js within an ionic framework application

Currently, I am working on a project that utilizes vue / vuetify and I am trying to merge it into an Ionic project. The original project is functioning well with vue version 3.4, whereas the Ionic project is using Vue version 3.3, which is not suitable for ...

What are some solutions for troubleshooting a laptop freeze when running JavaScript yarn tests?

Running the yarn test command results in all 20 CPU cores being fully occupied by Node.js, causing my laptop to freeze up. This issue is particularly troubling as many NodeJS/Electron apps such as Skype, MS Teams, and Slack are killed by the operating syst ...

React and Express failing to display content

After relocating my React frontend folder to my collaborator's Express backend folder, here is our updated file structure. https://i.stack.imgur.com/i77XJ.png This code snippet represents app.js which is responsible for rendering the website. const ...

What is the process for defining default prop data in Next.js?

Currently, I am in the process of developing a React/Next app with CRUD functionality. In regards to the update form, I have included the code below which is responsible for fetching existing data and updating it via an API. However, there seems to be a ma ...

Ways to customize the appearance of a react-chartist component

I recently created a React JS component that utilizes the amazing react ChartistGraph component. However, I've run into an issue where the default CSS of ChartistGraph seems to be conflicting with my custom styling. While there is plenty of documentat ...

Tips for customizing the appearance of a React-Table header when sorting data

How can I change the header's background color in react-table based on a selected item? For example, if I click on ID, the ID header should change its background color to red. I have tried various methods to update the background color upon selection ...

Incorporating a delay into looped HTTP requests while effectively utilizing Promise.all to track their completion

Greetings! In my current project, I am trying to introduce a 50ms delay before each subsequent HTTP request is sent to the server. Additionally, I aim to incorporate a functionality that triggers after all requests have been successfully made. To better e ...