Apply dynamic CSS class in Vue.js

Is there a way to assign a dynamic class in Vue.js using something similar to the following code?

<strong :class=`color-${category.level}`>{{ category.name }}</strong>

The above HTML is part of a loop, but I am having issues accessing category.value within that loop. Any suggestions on how to make it work?

Answer №1

It is important to remember that the class value should always be enclosed in either single quotes :class='...' or double quotes :class="...":

<p :class="`background-${section.color}`"></p>

Answer №2

Implement a new function within the :class attribute for your specific situation

 :class="switchClass(category.level)"

Next, define the following method in your script:

 switchClass(level) {
    return `color-${level}`
  }

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

Enhancing unique designs

After searching for a background pattern, I stumbled upon this unique design. Currently, I have it as the backdrop for my website. The issue arises from the fact that this pattern doesn't seamlessly repeat. When I set it as the body background usin ...

How can I pass my cookie token in a Next.js server-side component request?

My Next.js version is 14.1.0 and I am currently using the App router. async function Page() { const dataPromise: Promise<any> = getData(); const data = await dataPromise; console.log('data: ', data); return ( .... ); } The ge ...

Is there a way to retrieve the values of two attributes and assign them to a new attribute?

I am working on a calendar table with multiple attributes and I am curious to know if it's possible to combine all attributes into one. Here is an example code snippet in HTML: <div class="day_num" data-date="17" date="July 2019">17</div> ...

How can I resolve the "ReferenceError: $ is not defined" error when trying to use jQuery with Node.js on the server side?

Can someone assist me? I'm encountering an issue with using jQuery in my node.js app. Every time I attempt to use '$', I receive an error stating "$ is not defined" even though I have defined it at the beginning. Here's the process I fo ...

What are some creative ways to conceal an object?

const r1 = Math.floor(Math.random() * 255); const g1 = Math.floor(Math.random() * 255); const b1 = Math.floor(Math.random() * 255); $(".color1").click(function (){ $(this).css("background", "rgb(" + r1 + "," + g1 + "," + b1 + ")"); ...

What is the best way to retrieve the data from this date object?

How can I access the date and time in the code below? I've attempted using functions within the Text block without success. It's unclear to me what mistake I'm making or how to correctly access this object, or transform it into an object th ...

Choosing options from an API response in a REACT-JS application

I have a Select Component in my application and I want it to automatically show the selected data once the response is received. import Select from "react-select"; <Select menuPlacement="auto" menuPosition="fixed" ...

What are the best practices for managing POST data in Express.js?

Lately, I've been consistently encountering 500 errors from Express.js which seem to stem from a failure to retrieve a crucial string key required by the request. On the client side, numerous requests are being made to the same endpoint (/restore), a ...

Most effective method for waiting for a dropdown to load and choosing a value using Selenium in JavaScript

My current task involves interacting with a website built in React using Selenium to choose a value from a dropdown menu. Given that the website is built in React, I understand that waiting for the DOM to be ready may not always work as expected, but I st ...

Jquery issue: Lightbox unexpectedly closing by itself :(

Help needed: My light box is closing automatically within seconds. $(document).ready(function(){ $('.lightbox').click(function(){ $('.backdrop, .box').animate({'opacity':'.50'}, 300, 'linear'); ...

Tips for exporting 3D objects from 3ds Max Studio for optimal use in Three.js

I am facing an issue with loading a 3D object that I created in 3D Studio Max! When I export it as a .obj file (which generates two files, .obj and .mtl), I have tried using OBJMTLLOADET(), MTLLOADER(), and OBJLOADER() but none of them seem to work. Other ...

Can someone explain the meaning and syntax of this code in a clear way

I recently encountered this syntax in a tutorial and I'm unsure if it's ES6. It was part of a reduce function. Can someone provide a clear explanation of what is happening inside these parentheses? {...curr, ...acc} Here is the full code snippe ...

Unable to manage the rotation in Three.js GLTFLoader

Issue Why am I unable to rotate the angle of an object? (scene.rotation.x += 0.03;) The camera can, but the scene object cannot. I've searched for a solution for a long time without success. Here is my code: import * as THREE from 'https://un ...

Obtaining the address and port in Express.js without interrupting the execution

Despite reading numerous guides on Nodejs and Expressjs, I still struggle to grasp the concept of how it all comes together: Consider this simple Hello World application using Express.js (excerpted from http://expressjs.com/starter/hello-world.html). va ...

Loading data in a Bootstrap datatable can sometimes be a slow process

Hi there, I'm currently using a codeigniter bootstrap theme datatable to retrieve data from my database. However, the loading time is quite slow as it loads all the data at once before converting it into pagination. Is there any way to only load 10 re ...

Issue with showing multiple images on HTML page

I'm currently working on enhancing my webpage by enabling the upload of multiple images. However, I'm facing challenges in figuring out how to obtain a valid URL for the image source and to verify if the correct number of files have been uploaded ...

Vue template is not being rendered when served through Django

I am currently working on a Django application where Vue is used as the frontend to render templates. In my Django view code, I have the following components: # thing/views.py def index(request): template = loader.get_template('thing/index.html&a ...

Removing a specific item from a panel using JavaScript

Here is the code I have created that functions in a specific way. When a user selects a checkbox, it adds the selected item's name and price to calculate the subtotal based on the quantity inputted by the user. Now, when a user deselects a checkbox, ...

Remove an item from an array in JavaScript by specifying its value, with compatibility for IE8

Is there a way to remove an item from an array by its value rather than index, while ensuring compatibility with IE8? Any assistance would be greatly appreciated. Thank you. Below is the array in question: var myArray = ['one', 'two', ...

Are you experiencing issues with the .submit() function when used in conjunction with other

Currently, I am working on a survey form that incorporates JQuery to dynamically display or hide fields based on user selections. //FORM CONDITIONALS: This script is responsible for hiding all the helpfulness radios and only displaying them when "Yes" fo ...