The initial click does not trigger a Vue DOM update, but subsequent clicks do initiate the update

I developed a Vue application for a dynamic form with two buttons, and I need to indicate which button was clicked in the request. To achieve this, I update the value of a hidden input field and then submit the form using jQuery. However, when I click on the button that should set the hidden input to true for the first time, it sends false initially instead. Subsequent clicks work as expected.

Here is a brief excerpt of what I'm doing, excluding the actual jQuery request. The value seems to update correctly, so I'm unsure if it's a race condition or if I'm overlooking something obvious.

<form id="myVueForm">
  <input type="hidden" name="submitEdit" :value="submitEdit">
  <button type="button" @click="submitEdit = true">Submit Edit</button>
  <button type="button" @click="submitEdit = false">Submit without Edit</button>
</form>

var myApp = new Vue({
  el: '#myVueForm',
  data: function() {
    return {
      submitEdit: false
    }
  },
  watch: {
    'submitEdit': function(value) {
      console.log(this.submitEdit, "submitEdit value");
      console.log('Submitting form!');
    }
  }
})

https://codepen.io/anon/pen/jvGXLG?editors=1111

Answer №1

Vue's updating mechanism has an inherent asynchronous nature. After modifying its virtual DOM, any code that follows is executed immediately before the changes are applied to Vue's DOM. To ensure functionality after the update occurs, you must utilize .$nextTick() method.

'submitEdit': function(value) {
   this.$nextTick(function(){
      console.log(this.submitEdit, "submitEdit value");
      console.log('Form submission in progress!');
   });
 }

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

"Error: Unable to access the '$router' property of null" encountered in Vuetify

After attempting to navigate to a different page by clicking a button in vuetify, I encountered an issue. The console displayed the following error message: "TypeError: Cannot read property '$router' of null" Below is the code snippet: Product ...

When a button is triggered, it should initiate a click event on an input in TypeScript

I have created a color picker that is visible on a page. When clicked, it displays a dropdown menu of colors for selection. However, my objective is to hide the color picker initially and only reveal it when a specific button is clicked. This way, the dro ...

Adding an item to the EmberJS Data Store

Is there a way to add a record to the Ember Data Store without relying on the adapter? Whenever I use this.store.push({type: type, data: data}), the store always sets the hasDirtyAttributes flag to true. To work around this issue, I have been using this. ...

Security Error when using the JavaScript map function in FireFox

My current dilemma involves using a JavaScript code to extract the above-the-fold CSS from my websites. Surprisingly, it functions flawlessly on Google Chrome. However, when I attempt to execute it on Firefox, an infamous 'SecurityError' occurs: ...

How to italicize a portion of output using JavaScript

There are numerous inquiries on the internet and on this site regarding how to make italic fonts, however, none specifically address how to italicize only a section of a string. In my scenario, I have a form where I input the book title, author's nam ...

Rendering an array of objects in Three JS

I am encountering an error while trying to render an array of objects in a three.js scene. The error message from three.js reads as follows: three.module.js:8589 Uncaught TypeError: Cannot read property 'center' of undefined at Sphere.copy (thr ...

Having trouble retrieving JSON data from the open weather API

I'm working on a small website to display the weather of a specific city using an API. However, I am facing an issue where I can't seem to display the temperature from the response. Below are snippets of my JavaScript and HTML files: var ap ...

What is the best way to choose the initial element in every group using jQuery?

If we have the following HTML input: <p>A</p> <p>B</p> <p>C</p> <div>D</div> <p>E</p> <p>F</p> <p>G</p> We can select B, C, F and G using the following code: $('p + ...

Removing an item from an array within subdocuments when a nested id is located

{ "_id" : ObjectId("55a4b23636e6ba35079eb497"), "userName" : "David", "email" : "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e28683948b86a2858f838b8ecc818d8f">[email protected]</a>", "image" : " ...

The combination of loading and scrolling JavaScript code is not functioning properly on the website

I created an HTML webpage that includes some JavaScript code to enhance the user experience. However, I encountered an issue when trying to incorporate a load JavaScript function alongside the scroll JavaScript function on my page. The load script is posi ...

Tracking global click events in Vue.js can provide valuable insights into user interactions on your

While working with JavaScript, I was able to create this code for a popover. By clicking on the navbar-link element, the popover will appear or disappear. However, it would be great if I could close the popover by clicking anywhere on the screen (when the ...

Delete the class once the image has finished loading

Is there a simple way to use JavaScript to implement Bootstrap 5 placeholders for lazy loading each image I load? Bootstrap 5 provides a placeholder class that displays a "loading card" while an image is loading. I want to utilize this class until each im ...

When dragging a new connection in jsPlumb, the existing one should be automatically removed

After using jsPlumb, I was able to create a setup with the following features: Multiple nodes functioning like nodes in a unique flowchart design. Each node has a single target where connections can be dropped. Every node has zero, one, or more exits. Ea ...

Accessing an array from another method within the JavaScript class

Hello everyone, I am new to JavaScript and have a question about calling an array from another function within the same class. Here is an example code snippet: class Something { constructor () {} async functionA () { this.list = [] } a ...

Ways to bring in external javascript files in reactjs

I'm currently working on a form that requires the user to input their location. To achieve this, I have integrated the npm package react-geosuggest-plus. However, I want to avoid including <script src="https://maps.googleapis.com/maps/api/js?key=AI ...

Exploring jQuery: Techniques for Hovering, Changing, and Toggling Images

Currently, I am busy working on my project and I am attempting to achieve this by... I ideally want everything to be operational through click actions for each individual image, allowing them to have their unique "paper". I am aiming for a hover effect an ...

Using Angular service within InnerHTML functions

Within my Angular 10 application, I am utilizing innerHtml to display some content that includes anchor links. My goal is to trigger a function every time a link is clicked, which will then invoke an Angular service. In the code snippet below, I am attac ...

Can a single button click be shared across multiple forms?

The main concept involves a grid where when a user double-clicks on a row, a modal window (Bootstrap panel) opens with a panel-body section for editing the data and a panel-footer containing a btn-group for actions like "Save", "Cancel", or "Close". This s ...

Successive, Interrelated Delayed Invocations

There are two functions in my code, getStudentById(studentId) and getBookTitleById(bookId), which retrieve data through ajax calls. My ultimate goal is to use Deferreds in the following sequence: Retrieve the Student object, Then fetch the Book Title bas ...

jQuery will not carry out the initial trigger action more than once

I've successfully implemented a feature where clicking on one of the 3 divs causes the main container to slide half away by 600px and reveal a new container with additional options. However, I'm facing an issue where after closing the new content ...