Vuetify: The checkbox displays the opposite status of whether it is checked or unchecked

Can you help me simplify this problem:

In my Vue.js template using Vuetify components, there is a checkbox present:

<v-checkbox 
   v-model="selected" 
   label="John"
   value="John"
   id ="john" 
   @click.native="checkit">
</v-checkbox> 

The code for the checkit() method is as follows:

checkit: function() {
  let elt = document.getElementById('john')
  if(elt.checked) {
    console.log('checked')
  } else {
    console.log('unchecked')
  }
}

However, the result I am getting is opposite - it shows unchecked when checked and vice versa.

What could be causing this issue and how can it be resolved?

View Codepen demo

Answer №1

Before checking or unchecking the checkbox, the console.log is triggered. Here's what occurs:

  1. The user clicks on the checkbox
  2. The Javascript listener is executed
  3. If none of the listeners cancel the event, the checkbox is set as checked or unchecked.

To handle this, it's better to listen for the change event: https://codepen.io/anon/pen/VGOMPB?editors=1011

Another issue is relying on document.getElementById('john') to check the checkbox status. Why not use the selected property instead? The DOM is updated after JS execution to prevent frequent refreshes. To wait for the DOM update, you can utilize the $nextTick helper: https://codepen.io/anon/pen/BOewpg?editors=1011

checkit: function () {
    this.$nextTick(() => {
        let elt = document.getElementById('john')
        if(elt.checked) {
            console.log('checked')
        } else {
            console.log('unchecked')
        }
    })

    //this.selected.push('Vuejs')
    //this.selected.push('Paris')
}

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

Shapes in Three.js with multiple colors

As a newcomer to Three.js and webGL programming, I have successfully created a cone and a cylinder in my scene using the script provided below: var scene; var camera; var renderer; var createACone = function() { // Cone creation code here } var creat ...

Live notification application using node.js

I am looking to create a recipe maker webapp for practice purposes. This webapp will consist of 2 main pages: the index page and the recipes page. On the "index" page, users can create their own recipes. On the "recipes" page, users can view a table ...

Are ES6 arrow functions not supported in IE?

When testing this code in my AngularJs application, it runs smoothly on Firefox. However, when using IE11, a syntax error is thrown due to the arrows: myApp.run((EventTitle, moment) => { EventTitle.weekView = event => \`\${moment(event.s ...

Steps for integrating HLS video service with Vue3.js single page application

As I work on developing a video streaming platform using Vue.js, one particular challenge has come to my attention. When utilizing single-page application (SPA) frameworks like Vue.js, JavaScript code runs on the client's browser. This means that segm ...

Attempting to retrieve the current time using JavaSscript

const currentTime = new Date(); const hours = now.getHours(); Console.log(hours); This block of code is returning an error message... Uncaught ReferenceError: now is not defined Please note that this snippet is written in JavaScript. I attempted to us ...

Tips for creating a seamless merge from background color to a pristine white hue

Seeking a seamless transition from the background color to white at the top and bottom of the box, similar to the example screenshot. Current look: The top and bottom of the box are filled with the background color until the edge https://i.stack.imgur.com ...

Ways to shift placeholder text slightly to the right within a select dropdown?

Struggling with this issue for hours, I can't seem to figure out how to: Adjust the position of the placeholder text (Search) by 10 pixels to the right in my select component Reduce the height of the field (maybe by 5px) without success. Could someo ...

SCRIPT5007: The property 'href' cannot be assigned a value as the object is either null or undefined

This script is functioning properly in browsers like Chrome and Firefox, however it is encountering issues when used with Internet Explorer. An error message is displayed in the IE console: SCRIPT5007: Unable to set value of the property 'href': ...

jQuery setup for doWhen

Struggling to get doWhen functionality to work properly. Here is my index.html setup: <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> <script type="text/javascript" src="js/jquery.dowhen.min.js"></sc ...

How to showcase numerous PDF documents within a ReactJS application

As a newcomer to the world of React, I am facing an issue with displaying multiple PDFs based on the selected link. Although I have been successful in displaying a PDF from a list of links pointing to my stored PDFs within the src directory, I encounter th ...

Is anyone else experiencing issues with the Express middleware that checks for IDs? Looking for suggestions on how to fix

Currently working on a project with Node js utilizing Express and MongoDb for the backend. In this project, USERS have the ability to post COMMENTS, so I have created a middleware to access the DELETE route and check if the USER ID matches the ID of the in ...

Renewed Promises: Exploring Promises in Angular JS

Revised with HTTP and initial code inspired by requests/Refer to the end of the post: Lately, I have been seeking help from the amazing SO community as I navigate through my AngularJS learning journey. I used to be a traditional C programmer but recently ...

Determine which children should be displayed in a Kendo treeview based on the field titled "ParentId"

After converting a list of records into JSON format, I am looking to transform this data into a hierarchical KendoTreeView. Here is the JSON data: [ { "id": 1, "name": "A", "parentID": 0, "hasItems": "true" }, { "id": 2, "name": "B", "parentID": 1, "has ...

Locate elements within an array where a specific attribute includes the specified search term

While working with Javascript, I encountered an issue where the function I wrote to retrieve objects from an array was not returning all the data that met the query criteria. In my dataset, which originally contained 1536 objects, there are several jokes ...

Lambda functions are not activated by cognito events

I am currently working on setting up an authentication workflow using AWS Cognito to synchronize my users table (Hasura GraphQL backend) with Cognito users. However, I am facing an issue where the post-confirmation Lambda function is not triggering. Below ...

Step-by-step guide to performing an AJAX request in JavaScript while using Ubuntu

My current setup involves using a JavaScript file in conjunction with NodeJS to execute AJAX calls. To accomplish this, I have installed and imported jQuery as demonstrated below: var http = require("http"); $ = require("jquery"); test(); funct ...

How can I pass an array of string inputs into Vue 3?

Working with Vue 3, I'm setting up a form that will display text input fields corresponding to a fixed-length array of strings. Each field's v-model should connect to the respective string in the array. Here is my current code snippet for the vie ...

After the request.send() function is called, the request is not properly submitted and the page automatically redirects

I'm currently working on a basic ajax comment form that includes a textarea and a yes/no radio button. If the user selects 'yes', their comments are posted and then they are redirected to a new page. If the user selects 'no', th ...

Validation of forms in Angular using a pseudo submission method

On a webpage, there is a form with two buttons: one to calculate a price and the other to submit the form. Below is a basic example: <form novalidate name="formStep1"> <select ng-model="address" required> <option></option> ...

Effects with Cleanup - terminates ongoing API calls during cleanup process

Developing a React album viewing application: The goal is to create a React app that allows users to view albums. The interface should display a list of users on the left side and, upon selecting a user, show a list of albums owned by that user on the righ ...