The Vue.js checkbox linked to a v-model with a value set to false is currently marked as

I'm attempting to create a to-do list resembling the Eisenhower matrix, but I'm encountering an issue with v-bind and v-model being triggered incorrectly.

How can I resolve this bug? https://i.sstatic.net/iIhqR.png Code inspiration from Vue.js Todo App - Basics - Part 1

<div class="todoList" v-for="(task, index) in todo" :key="task.id">
                    <div>
                        <div v-if="task.type == 'notimportant'">
                            <input v-model="task.completed" type="checkbox" name="" id="" @change="completeTask(task)" />
                            <div class="task" v-bind:class="{ completed: task.completed }">
                                {{ task.title }}
                            </div>
                            <button class="removeTask" @click="removeTask(index)">X</button>
                        </div>
                    </div>
                </div>
export default {
    name: "app",
    data() {
        return {
            newTodo: "",
            type: "",
            important: "",
            id: 3,
            todo: [
                {
                    id: 1,
                    title: "Fist Task",
                    completed: "false",
                    editing: "false",
                    type: "urgent",
                },
                {
                    id: 2,
                    title: "Second Task",
                    completed: "false",
                    editing: "false",
                    type: "important",
                },
            ],
        };
    },

Full code available above

Answer №1

It seems like the issue arises from using the string false as "false", which actually evaluates to true. Keep in mind that an empty string '', a value of 0, null, and undefined are all evaluated as false, and vice versa.

To delve deeper into type coercion, check out this link here

let app = new Vue({
  el: "#app",
  data() {
    return {
      newTodo: "",
      type: "",
      important: "",
      id: 3,
      todo: [{
          id: 1,
          title: "Fist Task",
          completed: false,
          editing: "false",
          type: "urgent",
        },
        {
          id: 2,
          title: "Second Task",
          completed: false,
          editing: "false",
          type: "important",
        },
      ],
    };
  },
  methods: {
    addTask() {
      if (this.newTodo.trim().length == 0) {
        return;
      }
      this.todo.push({
        id: this.id,
        title: this.newTodo,
        completed: "false",
        editing: "false",
        type: this.type,
      });
      this.id++;
      this.newTodo = "";
    },
    removeTask(index) {
      this.todo.splice(index, 1);
    },
    completeTask(task) {
      task.completed = event.target.checked;
    },
  },
  directives: {
    focus: {
      inserted: function(el) {
        el.focus();
      },
    },
  },
})
* {
  margin: 0px;
  box-sizing: border-box;
}

.removeTask {}

.completed {
  text-decoration: line-through;
}

.task {
  text-decoration: 0;
}


/*
.conteiner {
    display: flex;
} /*
.todoBox {
    width: 50%;
    height: 50vh;
    border: 1px solid black;
    text-align: center;
}
.todoList {
    width: 100%;
    height: 100%;
    border: 1px solid red;
}
*/
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <div class="conteiner">
    <input placeholder="what we do ?" type="text" v-model="newTodo" />
    <button @click="addTask()">Add</button>
    <select v-model="type">
      <option disabled value="">Select</option>
      <option value="urgent">Urgent</option>
      <option value="noturgent">Not Urgent</option>
      <option value="important">Important</option>
      <option value="notimportant">Not Important</option>
    </select>
    <div class="todoBox">
      <h2>Urgent</h2>
      <div class="todoList" v-for="(task, index) in todo">
        <div>
          <div v-if="task.type == 'urgent'">
            <input v-model="task.completed" type="checkbox" name="" id="" @change="completeTask(task)" />
            <div class="task" v-bind:class="{ completed: task.completed }">
              {{ task.title }}
            </div>
            <button class="removeTask" @click="removeTask(index)">X</button>
          </div>
        </div>
      </div>
    </div>
    <div class="todoBox">
      <h2>Not Urgent</h2>
      <div class="todoList" v-for="(task, index) in todo" :key="task.id">
        <div>
          <div v-if="task.type == 'noturgent'">
            <input v-model="task.completed" type="checkbox" name="" id="" @change="completeTask(task)" />
            <div class="task" v-bind:class="{ completed: task.completed }">
              {{ task.title }}
            </div>
            <button class="removeTask" @click="removeTask(index)">X</button>
          </div>
        </div>
      </div>
    </div>

    <div class="todoBox">
      <h2>Important</h2>
      <div class="todoList" v-for="(task, index) in todo" :key="task.id">
        <div>
          <div v-if="task.type == 'important'">
            <input v-model="task.completed" type="checkbox" name="" id="" @change="completeTask(task)" />
            <div class="task" v-bind:class="{ completed: task.completed }">
              {{ task.title }}
            </div>
            <button class="removeTask" @click="removeTask(index)">X</button>
          </div>
        </div>
      </div>
    </div>
    <div class="todoBox">
      <h2>Not Important</h2>
      <div class="todoList" v-for="(task, index) in todo" :key="task.id">
        <div>
          <div v-if="task.type == 'notimportant'">
            <input v-model="task.completed" type="checkbox" name="" id="" @change="completeTask(task)" />
            <div class="task" v-bind:class="{ completed: task.completed }">
              {{ task.title }}
            </div>
            <button class="removeTask" @click="removeTask(index)">X</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

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

Firestore implementing batching for realtime updates with the onSnapshot 'IN' condition

Summary I'm currently working on developing a Chat List feature similar to those found in major social networks. However, I'm facing challenges with managing state in React Native due to a common issue with Firestore involving the use of onSnaps ...

Custom attributes given to Stencil web components in Vite/Vue3 will not trigger any reactions

Short backstory I initially set up my project with a vue-cli environment using Vue 2 and options-api. Recently, I decided to transition to create-vue, which is based on Vite with Vue 3 and Typescript. To incorporate web components from Stencil into my pro ...

Leveraging the power of AJAX and JSON to showcase dynamic HTML content pulled from PHP

I'm working on retrieving data from my PHP file, and it contains information in the columns Email, FirstName, LastName, and State. $query = 'SELECT * FROM users WHERE LOWER(Email) = :email'; $stmt = $dbh->prepare($query); $stmt->bindV ...

Encountering the error message "Angular 'undefined is not a function' when trying to define a component

My Ionic code was originally working fine, allowing the user to input a list. I decided to refactor it into a component for re-usability but encountered an error undefined is not a function on line 4 of the Javascript file. How can this issue be resolved? ...

Can anyone explain how to successfully implement AJAX data in a PHP file?

I've been struggling to transfer data from my JavaScript file to a PHP variable. I've spent hours searching for solutions, but none seem to apply to my unique code. Here's the JavaScript code I'm working with... $(function(){ ...

Watching for changes in a callback function - Angular 2

I'm currently working on implementing a callback function within a service class that needs to send data back to the component class. ChatComponent.ts export class ChatComponent implements OnInit { constructor( public _chatService : ChatService) ...

Encountering a "Parsing error: Unexpected token, expected ";" " when developing a React application with the provided code

I am currently working on developing a React application, and I have encountered an issue in my app.js file regarding the render function. Despite being new to JavaScript, I am unable to figure out why this error is occurring. Apologies if it is due to a s ...

Manipulate data in petite-vue beyond the application's scope

My exploration led me to petite-vue for its compact size and straightforward approach, perfect for my basic UI update needs on a webpage. I want to manage the visibility of DOM elements along with controlling class names and styles. With multiple JavaScri ...

What is the process of sending a file from a remote URL as a GET response in a Node.js Express application?

Situation: I am working on a Multi-tier Node.js application with Express. The front end is hosted on an Azure website, and the back end data is retrieved from Parse. I have created a GET endpoint and I want the user to be able to download a file. If the f ...

"Enhance Your Communication: Utilize setTimeout in Ajax

Hey there, I could really use some help with the setTimeout function in my code. No matter what I try, it just doesn't seem to work. I'm currently working on a chat system where I need to send and receive messages (testing by opening 2 browser ...

Why might the Bootstrap menu be malfunctioning?

The reality is that Bootstrap is functional and widely used. Sharing the code could make a big difference in getting assistance! <ul class="top-header-contact-info secondary-menu"> <li class="nav-item dropdown-toggle"> ...

My React app experienced a severe crash when trying to render an animation in L

Currently, I am working on a React application that was set up using Vite. I recently incorporated an animation using Lottie, and although I was successful in implementing it, I encountered a problem when navigating between different pages of my applicati ...

Tips for streamlining code using switch statements in vue.js

Is there a more efficient way to simplify this switch statement for handling 5 different cases? Can I streamline the process of updating the completion status based on each step? data() { return { stepOneIsCompleted: false, ...

The npm package for google-spreadsheet.js is experiencing issues and is not functioning correctly when trying to replicate the GitHub

After attempting to implement the basic example code provided at https://github.com/theoephraim/node-google-spreadsheet, I encountered an issue. For instance: const { GoogleSpreadsheet } = require('google-spreadsheet') const creds = require(&apo ...

Is there a way to modify my code to restrict users from liking a post multiple times?

I am currently working on a like system and I have made some progress. However, I am struggling to make it so that the likes only increment once. Does anyone have any insights or suggestions on how to achieve this? I have considered using session variables ...

Container holding a Material-UI drawer

Is it possible to set the material-ui drawer inside a container in reactjs? I have wrapped my app page in a container with a maximum width of 600px. I want the drawer to start within that container instead of on the body page (picture). The app bar positi ...

Adjust the initial slider according to the values displayed in the following four sliders

Having an issue updating the slider value in the first one based on selected values from four other sliders. The selected value should not exceed 50, which is the maximum value in the first slider. Link to Working Fiddle : Below is the HTML code : & ...

The CSRF token in Laravel Blade seems to be unreachable by Vue

Having a blade with the following code snippet. <meta name="csrf-token" content="{{ csrf_token() }}"> <covid-form> </covid-form> Inside the covid-form component, there is a form structure like this: <form @submit.prevent="send"&g ...

Using Javascript to Highlight a Single Row in a Table

Greetings esteemed members of the skilled community at StackOverflow, I must humbly ask for your expertise in solving a dilemma that I am currently facing. The situation is as follows: I have a table generated from an SQL query, and it is crucial for the ...

Tips for positioning divs on top of an image with Twitter Bootstrap

I'm having an issue with displaying an image and dividing it using bootstrap div columns. The problem is that the image is overlapping the divs, making it impossible to click or attach jQuery events to it. Here is the code I am currently using: #view ...