Add a class by utilizing the :class attribute

I reviewed the documentation, but I am still struggling to implement this in my project. Initially, I simply want to display a specific class using vue.js that I can later modify dynamically. I just need to establish the default behavior of that class, so I attempted the following:

HTML

<div class="row">
       <h1 class="text-center title"></h1>
       <div class="col-md-offset-1 col-md-10">
          <div :class="progressYou">asd</div>
       </div>
    </div>

    <div class="row">
        <div class="col-md-offset-1 col-md-10">
            <div :class="progressMonster">asd</div>
         </div>
    </div>
    

CSS

.progressYou{
        width: '100px';
        background-color: 'green';
        height: '50px';
    }

    .progressMonster{
        width: '100px';
        background-color: 'green';
        height: '50px';
    }
    

Javascript

new Vue({
        el: '#monsterGame',
        data: {
            incrementMonster: 0,
            incrementYou: 0,
            progressMonster: {
                'width': '100px',
                'background-color': 'green',
                'height': '50px'
            },
            progressYou: {
                'width': '100px',
                'background-color': 'green',
                'height': '50px'
            }

        }
    })
    

Does the CSS file need to be linked? Essentially, I want to create a class in my CSS and dynamically alter it using the objects defined in the JavaScript data section, but nothing is appearing on the screen. What could be the issue here?

Answer №1

Your usage of the `v-bind:class` directive is incorrect as you are passing style specifications instead of classnames.

Additionally, there are unnecessary quotes in your CSS code.

new Vue({
  el: '#monsterGame',
  data: {
    incrementMonster: 0,
    incrementYou: 0,
    className: 'progressMonster'
  },
  methods: {
    toggleClass: function () {
      this.className = this.className === 'progressMonster' ? 'progressYou' : 'progressMonster';
    }
  }
})
.progressYou {
  width: 100px;
  color: white;
  background-color: green;
  height: 50px;
}

.progressMonster {
  color: black;
  width: 100px;
  background-color: lightblue;
  height: 50px;
}
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.2.1/vue.js"></script>
<div id="monsterGame">
  <div class="row">
    <h1 class="text-center title">YOU</h1>
    <div class="col-md-offset-1 col-md-10">
      <div :class="[className]">asd</div>
    </div>
  </div>

  <div class="row">
    <div class="col-md-offset-1 col-md-10">
      <div :class="[className]">asd</div>
    </div>
  </div>
  <button @click="toggleClass">Switch</button>
  
  
</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

The animation in CSS seems to be malfunctioning, but strangely enough, the exact same code works elsewhere

Recently, I encountered a strange issue with my project. An animation effect that was working perfectly fine suddenly stopped working when I opened the project. To troubleshoot, I downloaded the same project from my GitHub repository and found that the ani ...

Vue.js: Exploring the issue of filtering using v-if and v-for

I'm encountering an issue with a computed property in Vue.js that is being used in a v-if statement. I've been struggling to find a solution or refactor the code. Any guidance on this matter would be greatly appreciated. Situation: Currently, I ...

What is the best way to adjust the height and width of a div when it comes into view through scrolling?

How can I automatically and frequently change the size of my div when it is scrolled into view? HTML <div id="contact" class="fadeInBlock"> <div class="map"> <div class="pointer"> <div class="dot"></div& ...

Effortless method to handle package.json configurations

Is there a better approach for seamlessly transitioning between using npm link and git, or another solution that caters well to both front end and back end developers? The dilemma I'm facing revolves around developing a website that utilizes multiple ...

Browsing with PHP/HTML on Safari from Mac Os 10.6.5 seems to run at a sluggish pace

This is the program: <?php require_once 'swift-mailer/lib/swift_required.php'; $name = $_POST['name']; $email = $_POST['email']; $form_subject = $_POST['form_subject']; $form_message = $_POST['form_message ...

I am looking to adjust the width of an element within an Iframe

In my single post, I have a social sidebar on the left side. Currently, the buttons in the sidebar are appearing in different sizes, but I want them all to be the same size as the "Twitter" button. To see the issue, please visit this link. I've tried ...

Scrollable Turbo Table experiencing a problem with the PrimeNG filter dropdown

When using PrimeNG Turbo Table, I am facing an issue where the filter dropdown is getting placed inside the scrollable table. The dropdown works perfectly fine without the scrollable table. You can see it in action here. However, when the table is scroll ...

The Vue.js error message "Unable to access property 'array_name' as it is undefined" indicates an issue with

I'm currently working on fetching data using Axios requests and storing it in an array. Below is the code I have been using: props: [ 'products', ], data: function () { return { algolia: '', pro ...

Selecting objects in Three.js using the camera but without using the mouse

I am working on a Three.js app where I need to determine the object that the perspective camera is focusing on. In order to achieve this, I consulted the raycaster documentation. Most of the resources I came across discuss using raycasting with a camera an ...

Having trouble retrieving data from the payload within vuex actions

I am currently utilizing Vuex for state management. Here is my store.js file: import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex); export const store = new Vuex.Store({ state: { loggedIn : false }, getters: {}, mutations: ...

CSS problem: Chrome scrolling overflow glitch (ghost margin/padding)

Hey there, I've run into a bit of an issue with two divs containing tables - one acting as a header and the other as a scrollable content area. Everything is working perfectly except for this mysterious border/margin that keeps appearing in Chrome on ...

Is it possible to utilize v-if and v-for outside of the HTML element?

My task is to assign the entire list to a variable if the condition is met, otherwise I must iterate through the list and assign each element separately. <ul> <li v-if="flag = true"> <myVueComponent v-bind: ...

What is the best way to sift through an array and extract only the values that correspond with a user's input?

In my HTML file, I have an input field and a button element within the body section. Here is how they are structured: <input type="text" name="searchBar" id="searchBar"> <button onclick="returnText()">Sub ...

Utilizing PHP to create a form via email

I have created this form and I would like to know if it is secure enough. I attempted multiple times to implement a captcha system, but unfortunately, it did not work for me. As I am still a student, please refrain from sending overly complex solutions. Q ...

What is the best way to arrange buttons in a row horizontally?

Desired Output I need help aligning the buttons as shown in the Desired Output image, but when I run the code, the buttons stack vertically, resulting in Output like this. I've included the HTML, CSS, and JS code below. As a beginner in UI design, I ...

The component you are trying to import requires the use of useState, which is only compatible with a Client Component. However, none of the parent components have been designated with the "use client" tag

I encountered an issue with the code snippet below in my Next.js app directory when utilizing useState: When trying to import a component that requires useState, I received this error message. It seems that the parent components are marked as Server Co ...

What causes top margins to collapse while bottom margins remain intact?

Upon examining the demonstration below, it is evident that in the left-column, the top padding edge of .clearfix-using_display-table (the yellow section) and .clearfix-using_display-table p (the silver part) are touching. However, on the lower edge, for so ...

The functionality of TypeScript's .entries() method is not available for the type 'DOMTokenList'

I'm currently working with a stack that includes Vue3, Vite, and TypeScript. I've encountered an issue related to DOMTokenList where I'm trying to utilize the .entries() method but TypeScript throws an error saying Property 'entries&apo ...

JavaScript allows users to input an array name themselves

When fetching rows from my database using AJAX, I transform them into an array with a variable identifier. Here is the PHP code: $query_val = $_GET["val"]; $result = mysql_query("SELECT * FROM eventos_main WHERE nome_evento LIKE '%$query_val%&apos ...

Setting up a web server with a cyclical challenge

I've encountered an issue while hosting my server.js file with the configured API on Cyclic. The deployment was successful, but every endpoint call is returning a status 500 error. Additionally, I have hosted the React front-end on github pages. I&apo ...