How to trigger a Switch when the input of a dynamic form changes in Vue.js

After I asked this previous question, another one came to mind: How to disable button if multiple form inputs do not change in Vue.js

Each form has a switch. When the form inputs are changed, the switch should go from an OFF state to an ON state. I achieved this by adding the following line to the watch method for each form:

this.items.forEach( (_, index) => {
  this.$watch(['items', index].join('.'), {deep: true, handler: (newVal, oldVal) => {
      this.changed.push(newVal.id)
      this.items[index].switch = true
     }});
  });

However, after setting this.items[index].switch = true, the switch turns on correctly; but if a user switches it back off by clicking it, the text does not update anymore. It remains stuck on ON. How can this be fixed?

Check out the CodePen demo here

Answer №1

Whenever you modify this model, setting the switch to true by adding this line each time is essential. A conditional statement can be used for this purpose as shown below:

this.items[index].switch = this.changed.includes(newOld.id)

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

What's the reason for switching from a custom tab icon to a React icon?

I've encountered a strange issue while working on my app - the tab icon keeps changing from the one I've set to the ReactJS icon whenever I navigate between different routes. I have linked the icon correctly in my HTML file, so I'm not sure ...

Error Encountered when Attempting to Change Forms Dynamically in CRM 2011: JavaScript Code Execution Problem

Upon form load, I check the type of the form and dynamically load the appropriate form based on that. It was working fine, but when the form reloads to the new form, I encounter a "JavaScript" error which says "cannot execute code from freed script" in Eng ...

Utilizing Angular2 to access NPM package (Googleapis)

I am currently developing an Angular2 application that utilizes Webpack for the build process. I want to implement a Google oauth login feature in my application, so I have added the googleapi package from npm. However, I am facing difficulties when trying ...

What is it about that that ticks off so many different boxes?

Hey there! I've been working on creating checkboxes within next js that iterate through each filter and its options. Fortunately, I managed to get it up and running smoothly. However, I'm encountering an issue where checking one option in a speci ...

Using Vue to Retrieve Values from a Django Dictionary

When using Vue, the expression {{ game.data }} will return the following data: [ { "game": 1, "turn": 1, "player": 1, "word": "trend", "score": 18 }, { "game": 1, "turn": 2, & ...

Adjust the zoom level of a webpage while printing using javascript

Hello, I have been trying to print in Landscape mode using JavaScript, but since there was no standard code available, I decided to rotate the page div 90 degrees to achieve the landscape orientation. However, this has proven to be unhelpful as it results ...

My Vue component seems to be missing in my Vue.js project

Here is the code for my header.vue component: <template> <div> <h2>this is header h2</h2> </div> </template> <script> export default { name: 'Header', data () { return { dat ...

Backend framework

Currently, I am in the process of developing a robust web application that heavily relies on JavaScript and jQuery, with ajax functionality included. Additionally, there will be a database in place, managed using mySQL with several tables. I'm undeci ...

How can I display a user's group memberships in keycloak-js?

Our website relies on keycloak for user authentication in a Vue application, where users are assigned to various groups on the Keycloak server. However, I am struggling to find a way to determine which groups a user is a part of. After examining Vue.proto ...

Creating a Node.js asynchronous setup function

I'm in the process of transitioning from Nodejs v12 to v14 and I've noticed that v14 no longer waits for the setup function to resolve. My setup involves Nodejs combined with Express. Here's a simplified version of my code: setup().then(cont ...

Looping through textboxes in JavaScript to compare values

Just to clarify, I'm not working with arrays in this scenario. Having said that, I need some assistance. I have developed a webform in PHP that retrieves data from a MySQL table and displays it using its own mysqli_fetch_array command. Within this lo ...

Establishing a standard flatiron-director route (within the element) using the polymer core-pages component

This particular query is closely linked with issues surrounding the usage of flatiron-director/core-pages SPA in conjunction with route-specific JavaScript functions and default routes. While the solution proposed may be effective, my limited expertise in ...

Using an external file to control the Angular $md-dialog

I have encountered a small issue while using md-dialog from Material Design that is causing me some trouble. The dialog serves as a form for creating a new record in the database, and I need its controller to be loaded from an external file. This is becau ...

Angular - ng-repeat failing to update when nested array is modified

Utilizing ng-repeat to render data fetched via a GET request that returns an array. HTML <div ng-controller="candidateCtrl" > <div class="table-responsive"> <table class="table table-striped"> <thead> ...

Eliminate any properties with values that exceed the specified number in size

:) I'm trying to create a function that removes properties with values greater than a specified number. I've searched through multiple resources like this question on how to remove properties from a JavaScript object and this one on removing pro ...

When the page loads, a JavaScript function is triggered

My switchDiv function in Javascript is being unexpectedly called when the page loads. It goes through each case in the switch statement, except for the default case. Does anyone know how to solve this issue? $(document).ready(function() { $("#be-button" ...

Having trouble with the functionality of the React Infinite Scroll component

Here is how I am implementing the react-infinite scroll component: const [hasMore, setHasMore] = useState(true) const [pageNumber, setPageNumber] = useState(1) const fetchDataOnScroll = async () => { try { const res = await axios.get( ...

Decode array in JavaScript

Hello, I'm currently trying to deserialize this array in JavaScript using the PHPunserialize module: a:7:{s:13:"varPertinence";a:4:{i:0;s:5:"REGLT";i:1;s:2:"13";i:2;s:2:"15";i:3;s:2:"16";}s:10:"varSegment";N;s:12:"varSSegment1";N;s:12:"varSSegment2"; ...

Convert a two-column layout on the web into a single-column layout for mobile devices, featuring dynamic

Is there a way to style this diagram with CSS that will work on IE11 and all major browsers? It seems like Flexbox doesn't support dynamic height. Do I need separate left and right columns for larger viewports and no columns for smaller viewports? ...

Why isn't my redirection functioning properly?

My code is having an issue with the redirect function being called too early, causing the last file of a batch upload to fail. I've been searching all morning and trying different solutions without success. function uploadFile(something, callback) { ...