How can Vue add a class to an element without using v-model:class?

I am faced with the challenge of connecting two elements that are located in different areas of the DOM. The goal is to create a hover effect on one element when the other is interacted with.

The first element is within a component and is rendered using the following code inside the component:

<button @mouseover="$emit( 'event-hovered', event.id )" @mouseout="$emit( 'event-not-hovered' )">
  Button text    
</button>

The second element is dynamically generated through code from an AmCharts demo:

createCustomMarker: function (image) {
    var element = document.createElement('button');

    element.className = 'map-marker the-id-' + image.id;
    element.title = image.title;
    element.style.position = 'absolute';
    element.dataset.target = "#id" + image.id;
    element.setAttribute('data-toggle', 'modal');

    image.chart.chartDiv.appendChild(element);

    return element;
}

In order to achieve the desired effect, I have implemented a @mouseover event that sends the id back to the main instance. In the main instance, I utilize JavaScript to locate the second element since directly interacting with Vue has proved problematic. However, this approach restricts me from applying classes to the dynamically generated second elements using the conventional v-model:class. A method in the main instance was attempted:

eventHovered: function( elementId ){
  let markerWithId = document.getElementById( 'id' + elementId );
  markerWithId.classList.add("event-hovered");
  console.log( markerWithId );
}

Upon logging markerWithId, the added class (event-hovered) is visible. Yet, it fails to reflect on the screen due to Vue controlling the DOM structure.

Therefore, the question arises - how can I properly manipulate the element within the DOM? Is there a more efficient way to accomplish this task?

Answer №1

To ensure the changes to the marker class are applied, remember to call the validateNow() function after making any edits. Here is an example of how to do this: https://docs.amcharts.com/3/javascriptcharts/AmCoordinateChart

According to the documentation: It is recommended to use the validateNow() method whenever you make modifications to a class. This will redraw the chart, with the option to include or exclude the validation of data and event skipping (both set to false by default).

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 could be causing the Angular form to refresh the entire page?

Two different forms are displayed on the same page: <form ng-submit="actionA()"> <input type="text"> <input type="submit" value="Submit"> </form> <form ng-submit="actionB()"> <input type="text"> <inp ...

Guide on automatically extracting table rows and generating an Excel spreadsheet

I am currently working on a script that dynamically adds the first row (TH) to a table and then exports it to an Excel sheet. However, I am facing an issue where instead of appending each row, the script keeps stacking on top of the next table row. Below ...

What could be causing my page width to only expand to 100% when using "fit-content"?

After searching extensively, I'm unable to find a solution that fits my current issue. My goal is to construct a practice ecommerce website using React. One of the components I have is a header which I'd like to occupy 100% of the screen width, c ...

How to implement a select all feature using React and MaterialUI

I am facing a challenge with managing multiple sets of checkboxes independently along with a toggle that can switch them all on or off within their respective groups. Each checkbox has two states, and I can identify the checkbox being clicked using event.t ...

Utilizing jQuery for serializing unorganized lists

I have created multiple lists within a list and I need to pass the IDs using jQuery to another PHP file (updateDB.php). I attempted to serialize the list data but was unsuccessful. I'm not certain if I have implemented it correctly, I searched extens ...

What is the best way to update a view in AngularJS?

When working on a web application, I encountered a scenario where a user is connected to multiple accounts, each containing transactions. Whenever the user switches their account, I need to fetch the new transactions associated with that specific account. ...

Tips for extracting value from PHP and transferring it to a jQuery function

I am trying to calculate the distance between two airports and pass this value back to a jQuery function to use it in further calculations. I'm unsure of where I went wrong? HTML: The 'dept' and 'dest' inputs fetch airport informa ...

Issues with fetching data from a Drupal module using an Ajax call

I have created a custom module in Drupal where the .js file is supposed to make an ajax call to a .module file. However, I am facing issues as the ajax call is not functioning properly. Can someone please assist me with this? Below is my .js file: // Jqu ...

Assign a CSS class individually to each item within a v-for loop

I've been experimenting with Vue.js and I'm attempting to dynamically change the class of specific items in a v-for loop based on a checkbox selection. <template> <div> <ul> <div :class="{completed: d ...

Restricting the frequency at which a specific key value is allowed to appear in JSON documents

Within my JSON file, there is an array structured like this: { "commands": [ { "user": "Rusty", "user_id": "83738373", "command_name": "TestCommand", "command_reply": "TestReply" } ] } I have a requirement to restrict the num ...

Using jQuery to fetch and read the source code of a specified URL

I'm facing an issue with extracting the source code of a website URL into a variable. Here is my current approach: <script type="text/javascript"> debugger; $(documnet).ready(function () { var timer = $.ajax({ type: ...

Styling text using SVG in HTML

I'm trying to underline a text element in SVG using the text-decoration attribute, but I'm running into an issue with the color of the line not changing. Here is the code snippet I am working with: <svg id="svg" viewBox="0 0 300 ...

Adding a color picker to a Kendo Grid

I am working with a kendo grid that has inline editing feature. I am looking to add a kendo colorpicker to a specific column. Is there a way to display the selected color even when the row is not in the editing mode? Does anyone have an example of how to ...

Importing an image from the public folder in a nested directory with Next.js

My images are stored in the public directory and all of my code is located in the src folder. Usually, when I try to import an image from src/page/page.js like: /image/logo/logo-dark.png, it works. But when I am importing images from the src/component/cor ...

"Utilizing Express's Jade middleware to efficiently handle and manage

Can you create a custom exception handler for errors in jade templates? For example: // server.js app = express(); app.set('view engine', jade); app.locals.js = function () { throw new Error('hello'); } // views/index.jade html != ...

Scrapy-splash seems to be encountering difficulties in extracting elements during the scraping process

My attempt to extract data from this Why is Splash not working here? Does anyone have a solution for me? Your assistance will be highly appreciated! ...

iPad problem resolved: Disable click hover and double-click issue

I'm experiencing a problem with my web application specifically on Safari for iPad. I have to click twice in order to actually perform the click on the <a> tag. The first click triggers a hover effect, similar to when you hover with a mouse on d ...

Concealing the toolbars on iOS 7 with the overlay technique

After researching on Stack Overflow and Google, I have been trying to find a reliable method to hide the toolbars on iOS 7 since the old scroll trick is no longer effective. I came across this resource: I attempted the following code: <!doctype html& ...

Regenerate main JavaScript files in Gulp whenever partials are edited

In my gulp task for javascript files, I included partial js files in the source and filtered them out to avoid building them unnecessarily. gulp.task("js", () => { return gulp .src([ src_js_folder + "*.js", src_js_f ...

Disabling a SELECT OPTION in AG-Grid: A step-by-step guide

Is there a way to disable a dropdown element in AG-Grid using React? I've seen examples like the one on W3Schools where you can disable an option in a dropdown, but I'm not sure how to implement this in AG-Grid. The dropdown I'm working wit ...