Is it possible to dynamically alter the href attribute using v-if in Vue?

I have a form where I added an alert, but after adding the alert, it still shows the same link to add another alert. I want to show a different link to add another alert after adding the first one, but I'm not sure how to do this.

<template>

  <div class="not-as-small form-text mb-1">
  <a
    href="#"
    data-tc-add-alert-date-btn
    @click.prevent="addAlert"
    class="text-cyan"
  >&plus; Add Alert</a>
 </div>
<template>

<script>
method: {
   addAlert() {
    this.managedAsset.alertDates.push({});
  },
}
</script>

Answer №1

To display the label dynamically, you can check the length of the alert list.

In this example, a span with the content Another is displayed when the array's length is greater than zero. This method can be used alternatively to toggling the entire content.

A computed property named getAlerts is utilized to retrieve the list of alerts added by the user.

new Vue({
  el: "#app",
  data() {
    return {
      managedAsset: {
        alertDates: [],
      },
    };
  },
  methods: {
    addAlert() {
      this.managedAsset.alertDates.push({});
    },
  },
  computed: {
    getAlerts: function () {
      return this.managedAsset.alertDates;
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <div class="not-as-small form-text mb-1">
    <div v-for="(alert, index) in getAlerts">
      Alert {{ index + 1}}
    </div>
  </div>  
  <a href="#" @click.prevent="addAlert" class="text-cyan">&plus; Add <span v-if="getAlerts.length > 0">Another</span> Alert</a>
</div>

You can also use v-html for the same purpose

new Vue({
  el: "#app",
  data() {
    return {
      managedAsset: {
        alertDates: [],
      },
    };
  },
  methods: {
    addAlert() {
      this.managedAsset.alertDates.push({});
    },
  },
  computed: {
    getAlerts: function () {
      return this.managedAsset.alertDates;
    },
    buttonLabel: function() {
      return this.managedAsset.alertDates.length === 0 ? '&plus; Add Alert' : '&plus; Add Another Alert'
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <div class="not-as-small form-text mb-1">
    <div v-for="(alert, index) in getAlerts">
      Alert {{ index + 1}}
    </div>
  </div>  
  <a href="#" @click.prevent="addAlert" class="text-cyan" v-html="buttonLabel">
</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

What is the best way to incorporate images from JSON responses into an HTML document?

I successfully managed to populate an HTML table with the json responses. However, I'm encountering an issue when it comes to displaying the images associated with the json variables. I'm only able to display the URL links for the images. Below ...

What is causing the 'this' variable to be different from the anticipated value?

I'm encountering a problem with this code snippet. The 'this' variable is expected to hold 'Object Chart' on the lines where 'console.log' is used, but instead, it contains 'path.line'. As a result, the referenc ...

How to efficiently upload multiple files simultaneously in Angular 10 and .NET Core 5 by utilizing a JSON object

I have a JSON object structured like this: Class->Students this is a basic representation of my TypeScript class export class Classroom { Id:number; Name:string; Students:Student[]=[]; } export class Student { Name:string; Age:number; Sex:string; Imag ...

When it comes to retrieving values from JSON objects in JavaScript, one of two identical objects will return the expected values while the other may

I encountered a perplexing issue with two identical JSON objects. When I use JSON.stringify(obj1) == JSON.stringify(obj2), it returns true. Despite both objects being accessible and inspectable in the console, I'm facing difficulty accessing the valu ...

Express req.files returns null when using jQuery FormData

For client side functionality, I am utilizing Bootstrap and jQuery. On the server side, my technology stack includes node.js and express. The form structure is displayed below: <form id="signupform" action=""> <h2&g ...

Revamp the props structure in Vue3 while preserving reactivity

When working with a component that retrieves API data and passes it down to child components, I encountered an issue. I attempted to split the information into separate ref objects for each child component but ran into a problem - reactivity was lost. The ...

JavaScript - Modifying several object properties within an array of objects

I am attempting to update the values of multiple objects within an array of objects. // Using a for..of loop with variable i to access the second array and retrieve values const AntraegeListe = new Array(); for (let i = 0; i < MESRForm.MitarbeiterL ...

How can I assign a specific class to certain elements within an *ngFor loop in Angular?

I have a situation where I am utilizing the *ngFor directive to display table data with the help of *ngFor="let record of records". In this scenario, I am looking to assign a custom CSS class to the 'record' based on specific conditions; for exam ...

What is the best way to continue looping until my variable matches the user's input? JavaScript

As of now, this nested for loop continues until both i and j reach 3. How can I reset the variables once they have reached their maximum value so that the loop can continue running? Alternatively, is my current approach incorrect? for (i=1; i<=3; i=i+ ...

Choice of product variations as a package

I am currently working on a project and here is the data structure I have set up for options and combinations: https://i.sstatic.net/K2utM.gif Options: "options": [ { "id": "96ce60e9-b09b-4cf6-aeca-83f75af9ef4b", "posit ...

Looking to implement a CSS effect that will hide content without removing the space it occupies on the page?

Is there a way to hide specific buttons within the right column of a 3-column table display? I am currently utilizing jQuery to achieve this function. The purpose behind hiding these buttons is to prevent any quick clicks that might interfere with the fa ...

Navigating to a destination with react-leaflet: step-by-step guide

As a newcomer to React and Leaflet, I am trying to create a feature where users can input coordinates, trigger an event on pressing enter, and then have the map fly to those generated coordinates. I am successfully generating the latitude and longitude coo ...

Click the link to expand the picture

@foreach (var item in Model) { <img src='ShowShowcaseImage/@Html.Encode(item.ProductID)' id='@item.ProductID' /> <b>@Html.DisplayFor(m => item.ProductName)</b> <a href="#" class="enla ...

Using JavaScript to toggle the iron-collapse property

I've implemented multiple <iron-collapse> elements with unique IDs, each one corresponding to a <paper-icon-button>. On screens wider than 650px, I can interact with the <iron-collapse>s using their respective buttons. But on narrow ...

Checking for the accuracy of the provided full name

There is a specific task at hand: The field labeled “First Name Last Name” must only contain 2 words, with each word being between 3 and 30 characters in length. Additionally, there should be only one space between the first and last name. The issue t ...

JavaScript library unsuccessful in transferring data to PHP script

I am facing an issue while trying to transfer variables from javascript to a php file for execution. The problem is that the php file is not being called or executed, even though I have confirmed that it works properly when tested individually. $(function ...

Tips for sharing data between React components without causing re-renders or relying on JSX, such as through a function within functional components

As a beginner in react, I have been struggling to find answers to this issue. I am trying to figure out how to pass data from one functional component to another in react without actually rendering the child component. Does anyone know a solution for this ...

Issue with Bootstrap carousel: image protrudes past boundaries of parent container

Struggling with setting up a bootstrap carousel on my page. I want the image to extend beyond the parent div, positioned at the bottom rather than the top. How can I achieve this without disrupting the default carousel behavior? Here's a sketch of how ...

Create an array populated with unclosed HTML tags that need to be rendered

I'm trying to display a simple list, but it seems like React is having trouble rendering unclosed HTML elements that are pushed onto the list. This results in an error: ReferenceError: el is not defined If I use single quotes (') bookingStat ...

What is the best way to transfer data between my express middleware functions?

Hello, I am new to working with Javascript/Node/Express and currently trying to interact with the Facebook Graph API. Below is an Express middleware function that I have set up: My goal is to follow these steps: Acquire a user authentication token fro ...