Creating a class that utilizes a data source from JavaScript

I am trying to create a data source list using JavaScript in vue.js

<div class="{{item.clockIn}}"></div>

The value returned by {{item.clockIn}} can be either true or false.

.true {
content: 'IN';
color: blue;
}
.true {
content: 'OUT';
color: red;
}

Unfortunately, this isn't functioning as expected. Would appreciate any assistance with this issue.

Answer №1

Initially, it appears that there is a typo in your class declaration. The second .true should actually be changed to .false. You can then implement something along these lines:

<div :class="item.clockIn ? 'true' : 'false'"></div>

By doing this, the class attribute becomes reactive and retrieves the value based on item.clockIn directly.

Answer №2

HTML Code Example:-

<div v-bind:class="{ 'active': true, 'inactive': false }"></div>

Vue.js Data Object:-

data: {
  active: true,
  inactive: false
}

CSS Styling:-

.active {
  content: 'ON';
  color: green;
}

.inactive {
  content: 'OFF';
  color: grey;
}

Expected Output:
If the condition is true

<div class="active"></div>

If the condition is false

<div class="inactive"></div>

Learn more about class and style in Vue.js

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 is the best way to connect v-model with a select element when dealing with nested foreach loops?

Whenever I use alert(this.property_credentials.district) in vue.js, it returns empty <select v-model="property_credentials.district" name="district" class="country selectpicker" id="selectCountry" data-size="10" data-show-subtext="true" data-live-searc ...

Ways to prevent a div containing a lengthy content string from extending beyond other divs

Check out my Codepen project here Here is the HTML code for my personal website: <div id="splash" class="divider"> <h1 class="text-center text-uppercase">vincent/rodomista</h1> <p class="text-center text uppercase">Full Stack ...

Global variable not being recognized in a function in Angular/JavaScript

I developed a service that retrieves movie information from the omdb API. However, when I attempt to utilize it in my controller and add the result to my movie array, I encounter an issue. TypeError: Cannot read property 'push' of undefined ...

Combining values from multiple arrays into a single array using JavaScript/Reactjs

I am working with an array that contains arrays as its values. My goal is to generate an output that includes all the array values. Please note: The input array and its elements (arrays) do not have a fixed length. Input: array = [ [a,b], [c,d,e], ...

Guide on merging the root route with child routes using react router v6

When a user visits "/", I want it to automatically redirect them to "/todo", but this functionality is not working as expected. let router = createBrowserRouter([ { path: "/", element: <Layout />, children: ...

How can one generate an HTML element using a DOM "element"?

When extracting an element from an HTML page, one can utilize a DOM method like .getElementById(). This method provides a JavaScript object containing a comprehensive list of the element's properties. An example of this can be seen on a MDN documentat ...

Tips for displaying line breaks in a textarea and transferring data from JavaScript to the content of textarea

Seeking user input in a <textarea>, displaying it on the DOM with an edit button and preserving line breaks. When the user clicks edit, the old data is shown in a new <textarea> with breaklines. Adding an item. <form name="add" ...

What is the proper way to utilize a class with conditional export within the Angular app.module?

This query marks the initiation of the narrative for those seeking a deeper understanding. In an attempt to incorporate this class into app.module: import { Injectable } from '@angular/core'; import { KeycloakService } from 'keycloak-angul ...

Sending Massive Real-Time Analytical Data

I am currently in the process of developing a dynamic analytics website using Asp.Net and C#. I am seeking advice on the most effective way to transmit data from the server to the client. Here are some options I have considered so far: Utilizing an Asp ...

I am facing an issue with Angular where the $http.get method is

It seems like there must be a small oversight causing this apparently simple problem. I have a function that interacts with the Spotify API to search for an artist. I know that by accessing the corresponding route using a standard URL, a result is returne ...

Are there alternative methods for handling routes in React, other than using the react-router-dom@latest library?

Currently, I am focused on a frontend project. One of the tasks at hand is to configure the network of routes. During my research, I came across react-router-dom@latest as a potential solution. However, I am curious to explore alternative options availa ...

Does the server consistently indicate that it has been checked?

Recently, I encountered an issue with my JavaScript code that adds a checked callback to checkboxes within a specific div and performs an ajax call. Here is the snippet of code: $("#filter").on('change', '[type=checkbox]', function () ...

AngularJS: Filtering one array with another array

I have a data structure structured in the following way: $scope.data = [ { title: "Title1", countries: ['USA', 'Canada', 'Russia'] }, { title: "Title2", countries: ['France', 'Germany&apo ...

Adding days to a HijriDate: A step-by-step guide

I need help with two text boxes. The first box is for selecting the StartDate from an Islamic calendar, which I am currently using jQuery Calendars Datepicker for. The second textbox, ExpireDate, should automatically be set to 60 days after the selected St ...

Is there a method to access a particular element within an array without using array[somenumber]?

I am currently using Mongoose to work with MongoDB and making GET requests with Axios from the client to the server. So, I am executing this call with Axios axios.get(route).then(res => { const races = res.data[0].InsideRaceResults ...

What is stopping this paragraph from being vertically centered with just one line?

After following instructions from another user, I successfully centered text vertically. However, I encountered difficulties when trying to center the entire content vertically. My attempt to enclose the text in its own div did not yield the desired result ...

Having trouble sending a GET request from the client to my backend route using axios in a React-Node.js/Express setup. Where did I go wrong?

Struggling with making an API request from my backend route (using nodes/express). I'm making an axios request from the client site using React. The express backend route works fine, so the issue must be in my client-side code. I've been stuck on ...

Export all entries without taking into account pagination limits

My current setup involves using Datatables with pagination. I recently integrated the Datatable.buttons library to enable an Export to Excel feature. However, I encountered a limitation where only the first 10 rows on the current page are exported due to p ...

Is it possible to meta-refresh a page for redirection?

When creating a webpage, I included a META tag like this: <META http-equiv="refresh" content="5;URL=http://www.google.com"> The issue is that mobile browsers do not support this meta tag. It redirects properly on web browsers, but not on mobile dev ...

Execute the script before the Vue.js framework initiates the HTML rendering

In order to determine if the user is logged in or not, and redirect them to the login page if they are not, I am looking for a way to check the user's login status before the HTML (or template) loads. I have attempted to use beforeCreate() and variou ...