Displaying elements when the class is available

Can someone assist me with a VUEJS project? I am looking to display a component based on the presence of a specific class.

STRATEGY

If (class a) is found, show (component a) If (class b) is found, show (component b) If neither (class a) nor (class b) is present, do not display anything

<section class="card" v-if="">
    <h3>{{title}}</h3>
    <p>{{subtitle}}</p>
</section>

Answer №1

You have the ability to achieve this

Model:

<section class="card component-x component-y">
  <div v-if="isXPresent"></div>
  <div v-if="isYPresent"></div>
</section>

Simply delete either component-x or component-y as needed

Code:

computed: {
  isXExist () {
     return window.document.querySelector('section.component-x')
  },
  isYExist () {
     return window.document.querySelector('section.component-y')
  },
},

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

"Webpack error: Including a comma within a string leads to a syntax problem when bund

I'm attempting to merge this file into my main JS. var constants = { height: 600, width: 400, default_bezier = "[ { \"startPoint\" : [51.6503017608354,203.464445873753], \"endPoint\" : [-52.41285540263849,202.372456432 ...

Javascript: having trouble with closures and event listeners not getting added

There seems to be a recurring issue that I suspect is related to closures. I have 3 buttons, but when I execute this code, only the last button receives an event listener. This suspicion leads me to believe there might be a problem with closures. Despite ...

Using Javascript and jQuery to convert text into a tag

I'm attempting to use JavaScript to send a message on twitch.tv by modifying the textarea in the chatbox. <textarea class="tw-textarea tw-textarea--no-resize " placeholder="Send message" data-a-target="chat-input" data-test-selector="chat-input" s ...

Error: Unable to access the 'name' property of an undefined variable

When running the code, I encountered a "TypeError: Cannot read property 'name' of undefined" error, even though when I console.log it, it provides me with the object import React from "react"; class Random extends React.Component { constructo ...

Executing PHP Functions with Script Tags

I am trying to use PHP to output the <script></script> tag. Here is the code I am using: <?php echo "test"; echo "<br>"; echo '<script src="http://mywwebiste./mycode.js" type="text/javascript& ...

The variable unexpectedly transforms into an undefined value within the callback of an Angular $resource method

Building a function in Node to query my mongoDB database and return results into an object is proving to be more challenging than expected. The issue lies in setting up the object correctly. Here's a snippet of the code: The Node Function for MongoDB ...

I am having issues with my map function in ReactJS

import React, { Component } from "react"; import { TextField } from "@material-ui/core"; import SearchResult from "./SearchResult"; class Search extends Component { constructor() { super(); this.state = { ...

Is Meteor rejecting div calls when not inside getJson?

Recently, I had a rather interesting encounter with Meteor.js. Let me walk you through my code snippet: Template.mytemplate.rendered = function(){ $.getJSON('http://ip-api.com/json/?callback=?', function(lingo){ $('.location').text(" ...

Retrieving exclusive npm packages from an npmjs user

I am currently facing a challenge with transferring all of our npm modules from npmjs.com to a new location. The issue is that our modules are saved under a private npm user account, making it difficult for me to programmatically access and consume all the ...

Reducer is unaware of my current state within react redux

I am currently developing an application that allows users to add items to a collection. The process involves the user inputting the name of the collection item, and the corresponding item is added to the collection. However, I encountered an issue when di ...

Is it possible to implement an automatic scrolling functionality in this code snippet?

https://codepen.io/hexagoncircle/pen/jgGxKR?editors=0110 Stumbled upon some interesting code to tinker with and came across this piece. Attempted to search online for solutions, but my grasp on React (or maybe Java?) is close to zero. Tried following guid ...

Using Vue.js and Laravel's permission system

Currently, I am working on the integration of Laravel Permission API with Vue.js frontend. My chosen library for Laravel Permission is from https://github.com/spatie/laravel-permission. However, I am facing a challenge in understanding how to verify perm ...

Issues with boxes showing or concealing depending on the chosen option

I am encountering an issue with a dynamic dropdown feature. The secondary box is supposed to appear based on the selection of the first box. However, as soon as I select an option in the second box, it disappears immediately. I suspect the problem lies wit ...

How can I reverse the names displayed in ng-repeat when I click?

When utilizing the orderby filter in angularjs, I want to be able to sort the data only when the button is clicked. If the button is not clicked, the sorting order should not be displayed. <tr ng-repeat="tools in toolsfilter | orderBy:orderByField:reve ...

Number entered isn't visible in Bootstrap modal window

Can anyone help me troubleshoot why the value "APno" is not appearing next to "Appointment Number" in a Bootstrap modal despite the modal showing up? This is my code: Form <form method="POST> Appointment Number:<br> <input type="n ...

Material UI - Array of Chips

I have been working on creating a ReactJS component that displays an array of chips with unique styling for each one. To achieve this, I created individual makeStyles classes for the chips. However, I encountered difficulties in dynamically changing the cl ...

To handle a 400 error in the server side of a NextJS application, we can detect when it

I'm facing a situation where I have set up a server-side route /auth/refresh to handle token refreshing. The process involves sending a Post request from the NextJS client side with the current token, which is then searched for on the server. If the t ...

Retrieving Outdated Information through the Express Framework

Whenever I make a fetch request to a node express server, it sometimes returns old data that was previously fetched from the same endpoint. This issue occurs sporadically but seems to happen more often than not. Even after disabling Cache-Control in the f ...

Mastering intricate data structures using React.js

I am working on creating a table for orders using React+Redux. The data I need is stored in props and it has a structured format similar to this: [{ //stored in props(redux state) "id": 37, //order 1 "content": { "items": { " ...

When working with Angular Universal, using d3.select may result in a "reference error: document is not defined" in the server.js file

I'm currently working on an Angular project that uses server-side rendering to generate D3 charts. Each chart is encapsulated within its own component, such as a line-chart component which consists of TypeScript, spec.ts, HTML, and CSS files for rende ...