The JSON data button click function in Vuejs seems to be malfunctioning

I stumbled upon this interesting question, but unfortunately it didn't provide any solution.

"element": [
  {
   "value": "<button @click='changeTheme()' class='theme-link btn btn-light'>Default</button>",
   "class": "text-success"
  }
]

I have connected the JSON data with my Vue Component in the following way:

<p v-else v-html="element[0].value"></p>

Now, I am attempting to trigger this method. However, it's not working as expected!

methods: {
  changeTheme() {
     alert("Y");
  }
}

Answer №1

Consider moving the action away from the button and assigning it to a different key on the object, then trigger it upon clicking the element:

"element": [
  {
   "value": "<button class='theme-link btn btn-light'>Default</button>",
   "class": "text-success",
   "action": function () {alert("Y")}
  }
]
<p v-else v-html="element[0].value" @click="element[0].action"></p>

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 solution to resolving misaligned CSS divs

I am facing an issue with displaying 3 div cards that contain different sets of information. Despite setting them to display:grid and trying to evenly distribute the fractions, the divs are stacking awkwardly. JS fiddle demo <!--Main Background-- ...

Using a v-for to dynamically generate srcset properties

While I may be lacking some fundamental vue.js knowledge, I have a specific question regarding the <picture> element and more specifically, the srcset attribute. My API provides me with different formats of the same image as follows: "posts": [ { ...

Checking for the current loading status of a JavaScript file: How it's done

Currently, I am automating the GUI of my website using Selenium (RobotFramework). However, I have encountered an issue: Occasionally, when my automation script clicks on an element such as a button or link that is supposed to trigger an action, nothing ha ...

Investigating methods for troubleshooting unresponsive JavaScript that is failing to retrieve information from MongoDB

Currently, I am actively engaged in the steps outlined in this helpful tutorial: Data Visualization Tutorial Despite successfully troubleshooting various issues along the way, I am encountering a challenge where the data from MongoDB is not being displaye ...

What is the process for installing a third-party library in React Native without using npm or yarn?

Is there a way for me to install a third-party library manually without affecting the build run or performance of my project? I am looking to add this specific library: https://github.com/asmadsen/react-native-unity-view ...

Determine the maximum width of the longest word within the html content

The objective is to identify the broadest word within this content. This text comprises a sentence made up of words styled in various fonts, as displayed in the illustration. Below is the HTML snippet: <span style="font:bold 14px Verdana;">LONGES ...

Filtering substrings in an Angular data resource

DEFAULT_RECORDS = [{ id: 1, name: 'John Evans', number: '01928 356115' },{ id: 16, name: 'Murbinator', number: '053180 080000' }]; - retrieveEntries: function (name) { var foundRecords = {}; ...

Vue triggered events onEnable and onDisable

I'm working on my Vue application and I have a requirement to monitor the enable/disable status of an element (linked to a function) and based on that, trigger events for onEnabled/onDisabled to clean up certain data nodes. Is there a listener similar ...

Exploring the process of retrieving a JavaScript variable within a JSP page

Incorporating JavaScript code into a JSP file is essential for my project. The JavaScript file has been successfully added to the JSP page. The JavaScript file named abc.js consists of the following line of code: var dynamicDiv = 'Your leave is appr ...

What is the reason behind JSLint's preference for x === "undefined" over typeof x == "undefined"?

I'm feeling lost when it comes to JSLint. Initially, my code checked if div:jqmData("me") was undefined in this way: if ( typeof el.jqmData("me") == "undefined" ? el.not(':jqmData(panel="main")').length > 0 : el.not(':jqm ...

What could be causing only the initial CSS class in my project to be functional while the others are not?

Currently, I am facing an issue while using React with CSS. It seems like my CSS styles are not being applied correctly. Let me illustrate the problem. In my "App.js" file, I have the following code: import './App.css'; function App() ...

Verify if the link includes https:// while using angularjs

In my angular app, there is a lot of data stored in JSON format. [ {"link": "https://stackoverflow.com"}, {"link": "https://stackoverflow.com"}, {"link": "id-aW783D"}, //This data is incorrect {"link": "https://stackoverflow.com"} ] However, the ...

Contrasting import and require methods

// script.js file; let x = 0; let updateValue = () => { x = 1; }; export { x, updateValue }; // script1.js import { x, updateValue } from './script.js'; updateValue(); console.log(x); // --> x = 1; however // script.js let x = 0; let ...

The scenario of two users simultaneously gaining control access in socket.io creating a race condition

There is a need to ensure that only one user at a time is notified for an available room. I am implementing a solution to prevent multiple users from being notified simultaneously for the same room. socket.on('Check', function (room) { io.in(r ...

Enhance the appearance of TreeGrid nodes by customizing the icons according to the data within

Currently, I am working with the MUI DataGridPro component and my goal is to customize the icons of the TreeGrid nodes based on the data. This image illustrates what I hope to achieve: https://i.stack.imgur.com/nMxy9.png Despite consulting the official do ...

Update the value variable using a search/filter dropdown option

In my project, I have implemented a search/filter dropdown feature that uses the array of values returned by the allNameMuseums() method: ["ACQUARIUM", "Museo2", "Museo3"]. <h2>Search/Filter Dropdown</h2> <p>Click on the button to open t ...

The mousedown event handler in Three.js disrupts the focus on the input field

Here is a line of code in threejs: document.addEventListener('mousedown', onDocumentMouseDown, false); This particular code snippet causes the input field to lose focus when clicked. This can be problematic, for instance, when there is a canva ...

Is there a powerful alternative to ThreeJS that can be integrated into Android apps?

I am in the process of creating a website that allows users to personalize their jewelry online. To achieve this, I am utilizing ThreeJS. My next step is to develop an Android app, which requires me to create an API-only app for both the website and the An ...

Why do variables maintain the same value across modules when using the require function in Node.js?

We have a scenario where we have multiple files in the same directory: data.js var anArray = [1,2]; module.exports = anArray; mod1.js var data = require('./data'); module.exports = data; mod2.js var data = require('./data'); modul ...

Vue v-for is not displaying any items

This is my initial foray into Vue.Js. I am facing difficulties in displaying my nested object with the v-for directive. Whenever I use {{element.fist_name}} within the v-for, it just shows an empty list. Feel free to check out my code on jsfiddle. https: ...