Implementing a watcher property in JavaScript to dynamically add a class to an element

I'm currently facing an issue where I need to apply a class to an element when a certain data property changes. My approach involves using a watcher to monitor the value change and adding a class through JavaScript, as demonstrated in the code snippet below.

 <div class="ad-video" >
      
watch: {
  testAdActive() {
   document.querySelector('.ad-video').classList.add('ad-resize');
  }
}

Unfortunately, it seems that the above code is not functioning as expected, with the class 'ad-resize' failing to be added. Any suggestions on alternative approaches would be greatly appreciated.

Thank you, Jyoti

Answer №1

To tackle this issue, consider utilizing class-binding:

<div :class="{ 'ad-video': testAdActive }">

</div>

When the variable testAdActive is true, the class .ad-video will be added to your div.

The usage of : indicates v-bind.

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

Woocommerce - [product_categories] shortcode - Can I exclude specific categories from being displayed in the list?

Is there a way to hide specific categories (only two in this case) that are displayed using the [product_categories] shortcode? I have attempted to place these categories at the bottom of the list and used CSS to target them: .home .woocommerce ul.product ...

The onChange Event triggers only once in a checkbox input

I am implementing a checkbox component that emits an event to the parent component. However, in the parent component, I am facing an issue where I need to perform different actions based on whether the checkbox is checked or not, but it seems to only work ...

links contained within a single span inside an anchor tag

I am attempting to place all three spans within an anchor tag, inline. I am not certain what is missing from the code: a.facebook-button { float: none; display: inline-block; margin-bottom: 5px; left: auto; text-shadow: 0 -1px 1px rgba(0, 0, 0 ...

The d3 drag functionality is only active when the axis ticks are selected

Currently, I am developing a unique package that combines Angular and D3 (v3) and my main focus is on integrating D3's dragging feature into the package. Although I am very close to achieving success, there is a minor bug that I need to address. When ...

An effective way to simulate an axios call within a method

I'm attempting to simulate an axios call inside a vuejs method. Can this be achieved? Below is my vue component (SomeObj): methods:{ callAxiosMethod() { const callApi= axios.create(); callApi.defaults.timeout = 10000; ...

Detaching jQuery event listeners

Suppose I have two event handlers of the same type attached to the same object. The following example shows the mousemove event being attached to the window object. $('body').on('mousedown', '#uiScrollbar', function(e) { v ...

How do you trigger the playback of a specific audio file when a user clicks on it?

I'm currently working on an interactive music app that mimics the functionality of a piano. Users are able to click on different boxes on the screen, triggering a unique musical note to play. While I initially considered manually collecting all the I ...

Other Options for Delaying Execution in Node.js

Objective Exploring potential alternatives to improve the accuracy of timing functions in node.js, particularly focusing on a replacement for setTimeout. Background While developing a QPS (Queries Per Second) tester application, I encountered issues wit ...

Choosing the right framework for implementing push notifications can be a critical decision. Should

I am currently working on a Java application that requires the server to send push notifications to the client every one second. To achieve this, I am using HTML5 server-sent events for one-way communication from the server to the client. However, my conce ...

Content within the Grouped Bar Graph Bar D3.js

I referred to https://bl.ocks.org/mbostock/3887051 and customized the bar chart by adding text inside each bar representing a count, such as Upstream is 50, so the number 50 should appear in the bar itself. Despite trying solutions from resources like D3. ...

Discrepancy in functionality between .show() and .append() methods within JQuery

I have a container with an ID of "poidiv" that is hidden (display: none) initially. My goal is to dynamically load this container multiple times using a loop, where the maximum value for the loop is not predetermined. I attempted to achieve this using jQue ...

Having trouble with typecasting in Angular 9 after receiving an HTTP response?

When initializing my component, it fetches student information from an API. Here is the ngOnInit code for component-version1: ngOnInit(): void { if(!this.student) { this.studentsService.getStudentDetail(this.id).subscribe( (response: Stu ...

Create a button that matches the dimensions of the background image

I am working with a form that includes a submit button featuring a background image. <button type="button" class="button" alt="Send" onclick="validate_form_newsletter_wide( form )"></button> Everything works well when I define the dimensions ...

Creating a new object through manipulation of existing objects

In my attempt to transform an existing object into a new object structure, I am facing some challenges. Here is the current data set: const jsonStructure = { "a11/a22/animations": "snimations", "a11/a22/colours": "sl/colours", "a11/a22/fonts" ...

Executing Webpack + Vue on a Personal Virtual Server

Operating System: Mac OSX High Sierra Node Version: v10.15.0 NPM Version: 6.13.6 webpack Version: webpack-dev-server Version: Browser: Chrome The virtual server I'm using is which points to xampp/htdocs/lab. I have the project located in xampp/htdo ...

Looking to retrieve just your Twitter follower count using JavaScript and the Twitter API V1.1?

I am trying to retrieve my Twitter follower count using JavaScript/jQuery in the script.js file and then passing that value to the index.html file for display on a local HTML web page. I will not be hosting these files online. I have spent weeks searching ...

What steps should I take to create a React component in Typescript that mimics the functionality of a traditional "if" statement?

I created a basic <If /> function component with React: import React, { ReactElement } from "react"; interface Props { condition: boolean; comment?: any; } export function If(props: React.PropsWithChildren<Props>): ReactElement | nul ...

Sending an object to a component and changing its properties to be mutable

Is there a way to pass a Contact object into my ContactDetailsComponent so that I can use the component for both displaying and editing the Contact? I attempted to pass the object as a Prop, but I'm struggling to edit it. Here is what I've tried ...

Fixing the Jquery Clone Bug

Recently, I came across a jQuery clone plugin that is designed to fix the values of cloned textarea and select elements. This code snippet showcases how it works: (function (original) { jQuery.fn.clone = function () { var result = ori ...

Is there a way to create a button in an HTML project that will launch the user's email client?

Looking for some guidance on coding a homepage with HTML and CSS as I navigate my way through the world of web development. Take a look at this screenshot for reference: In the Jumbotron section, you'll notice that I have integrated a couple of boot ...