Modify the style of a newly created div by clicking on it

Exploring vue-cli and encountering a challenge.

I've created a row of 24 div elements with the following code:

 <template>
   <div class="row">
     <div class="hour" v-on:click="colorize" v-for="n in 24"></div>
   </div>
 </template>

My goal is to change the background color of the clicked hour div based on a value stored in VueX, but the key issue lies elsewhere.

Below are my methods :

methods: {
  colorize() {
    if(this.$store.state.picked === 1) {
      this.style.backgroundColor="rgb(103, 103, 103)";
    }
  }
}

The store functions correctly, but I suspect there's an error in how I'm using the 'this' attribute.

Any suggestions? :)

Answer №1

this in this context is referring to the Vue instance, not the element that was clicked. Therefore, you need to adjust your code like this:

 methods: {
   colorize(event) {
     if(this.$store.state.picked === 1) {
       event.target.style.backgroundColor="rgb(103, 103, 103)";
     }
   }
 }

new Vue({
  el: '#app',
  data() {
    return {
      elements: [
      {content:'content 1',tooltip:'tool tip 1'},
      {content:'content 2',tooltip:'tool tip 2'},
      {content:'content 3',tooltip:'tool tip 3'},
      {content:'content 4',tooltip:'tool tip 4'},
      ]
    }
  }
, methods: {
   colorize(event) {

       event.target.style.backgroundColor="rgb(103, 103, 103)";

   }
 }
})
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js></script>

<div id="app" data-app>

 <div class="row">
     <div class="hour" v-on:click="colorize" v-for="n in elements">
     {{n.content}}
     </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

First, I am populating an array with values. Then, I am transferring those values to a textarea for display. I want to ensure that duplicate names are removed from this

If there are duplicate names in the SkillIds array, I should remove those names. let skillIdsArray = []; $('#SkillSets table tr :checked').each(function () { skillIdsArray.push($(this).data("id")); }); $('#textarea').val(skillIdsA ...

Come together with Array starting from the specified startIndex and ending at the

I'm curious to know if there is a utility function available that allows for joining arrays while also providing an index. Perhaps Prototype or jQuery already have this feature, but if not, I am willing to create it myself :) What I envision is somet ...

Is it possible to permanently alter HTML values with JavaScript?

Can a html value be permanently modified using javascript? I am trying to edit a local file. Below are the codes I'm using: function switchPic(){ top.topPage.activeDef = top.topPage.document.getElementById('h1'); top.topPage.activeDef. ...

Guide to embedding a component within another component using route based rendering

My routing configuration looks like this: <Provider store={store}> <BrowserRouter> <Route path="/" component={App} /> <Route path="/customers" component={Customers} /> <Route path="/tickets" componen ...

Creating a menu prior to the initiation of my trio of programs

I recently started working with three.js and I'm looking to create a menu on a page before the program actually loads using three.js. I want to be able to display options like "start" and "music control" before the game itself is fully loaded. Would ...

The graph from Flot is not showing up, and there are no error messages

Recently, I have been experimenting with creating a graph plot using flot. However, I've encountered an issue where the graph is not displaying as expected. Despite using developer tools and JSlint to check for errors in my JavaScript code, both tools ...

An issue occurred while trying to establish the referrer policy

I encountered an error in my Chrome console while working on a WordPress site. The following error message showed up: "Failed to set referrer policy: The value 'http://example.com/comic/' is not one of 'always', 'default' ...

What could be causing my component to not update after I modify the states?

When I make an ajax request and update the state with the response data, my list of items does not rerender as expected. Even though the state is updated successfully, the changes are not reflected in the UI. export class Tiles extends React.Component { ...

Accessing XML content within a web browser

I am facing an issue with parsing a string in JavaScript. var output = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><abc><xyz><xyzResponse><URL>http%3A%2F%2Flocalhost%3A8080%2Fnet%2Fxyz.do%3Fpartner%3Ddummy%26 ...

Change element position to relative while scrolling

I created a wrapper with an animation similar to the one on Apple's Airpods Pro page. It features a video that plays gradually as I scroll, with the text smoothly scrolling over it within a specific area (text-display). So far, this part is working w ...

The system is unable to accept the Object ID

I am encountering an issue with displaying the Object ID from MongoDB in the console in VS Code. I am trying to retrieve the Object ID for the user who is currently logged into my application. I have created a hidden input tag with an id of ObjectID. <! ...

Using Vue.js: applying a CSS class to a child element within a component

Is it possible to insert a class into a component class tag from the HTML file? For instance, if we have a component like the one below and want to replace the icon field with whatever is used in the HTML file. Sample code from MyButton.vue file: <temp ...

Verify that the string does not have any repeating characters

I need assistance with a code that checks if all characters in a string are unique. I've noticed that the current code always returns true, which seems to be due to the false output of the if condition unless the first two characters in the sorted lis ...

Ways to Customize the new Date() Function to Display Only Specific Fields

I am attempting to format my this.state.date to appear as DD/MM/YYYY. To achieve this, I utilized the library found at https://github.com/mmazzarolo/react-native-modal-datetime-picker Currently, I can select my desired date, however, when I assign it to t ...

Restrict the selection of dates in the jQuery UI datepicker by disabling public holidays, weekends, the next day after 10am, and only allowing Tuesday, Wednesday, and Thursday as

I found a code snippet on disabling weekends, public holidays, and the next day after 10 am using JQuery UI Datepicker. However, I'm facing an issue where I want to restrict selections to only Tuesday, Wednesday, and Thursday. // JavaScript logic for ...

Troubleshooting issues with custom-switch in jquery.repeater with bootstrap

Hey there! I'm having an issue with the jquery form repeater plugin and bootstrap combination. Specifically, I have a form element that is part of the repeater form: <div class="custom-control custom-switch"> <input typ ...

Expressjs route encountering issue with imported database function failing to return a value

Currently, I am in the process of creating a REST API using Expressjs. Initially, all routes were integrated into one main file. However, I have now separated these routes, database connection, and database methods into their individual files. login-db.js ...

Is the navigation component's loss of properties due to a React router error?

After implementing react router for the first time, I noticed that the props passed to my nav component get lost once a new route is rendered. It's like the items in the cart are disappearing when I click checkout, but reappear correctly when I go bac ...

Node version 6.11.0 experiencing JavaScript heap error even with zero memory usage in the program

Encountering an out of memory error even though only two variables (i & j) are being used. Can someone please provide clarification? I am not storing anything in memory or saving it to any storage; whatever is generated is outputted to the console and the ...

How to achieve the functionality of ocibindbyname in JavaScript

I am currently utilizing an HTA page that is coded in JavaScript to monitor various Oracle tables. My goal is to optimize the Oracle query caching by using bind variables, similar to how I implemented it in a PHP environment with this code: $sql = "selec ...