I am currently new to Vue.js and am incorporating Bootstrap into my projects. I am unsure about how to customize buttons using the props id, such as distinguishing between element id and data-bs-target

When working with PHP, I am accustomed to iterating Bootstrap buttons and echoing variables inside element IDs. For example:

<button id="btn<? echo $i; ?>"/>

I do this for the element ID, data-bs-target, aria-controls, and aria-labelledby when using accordion buttons. However, I am unsure of how to pass props inside those elements' attributes.

Here is what I had in mind for the component:

<script setup>
   defineProps(['id','company','dates','position','desc'])
</script>

<template>
   <div class="experience">
      <div class="accordion-item">
         <h2 class="accordion-header" id="heading{{id}}">
            <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapse{{id}}" aria-expanded="false" aria-controls="collapse{{id}}">
               <div class="col-3"> {{company}} </div>
               <div class="col-5"> {{dates}} </div>
               <div class="col-3"> {{position}} </div>
            </button>
         </h2>
         <div id="collapse{{id}}" class="accordion-collapse collapse" aria-labelledby="heading{{id}}" data-bs-parent="#accordionExample" style="">
            <div class="accordion-body">
               <p> {{desc}} </p>
            </div>
         </div>
      </div>
   </div>
</template>

<style scoped>

</style>

Answer №1

If you want to utilize dynamic attributes, simply add a colon before : or use v-bind:. You can also concatenate a static string with a dynamic id using a template string.

For instance:

:id="`heading-${id}`"
<script setup>
   defineProps(['id','company','dates','position','desc'])
</script>

<template>
   <div class="experience">
      <div class="accordion-item">
         <h2 class="accordion-header" :id="`heading-${id}`">
            <button class="accordion-button" type="button" data-bs-toggle="collapse" :data-bs-target="`#collapse-${id}`" aria-expanded="false" :aria-controls="`collapse-${id}`">
               <div class="col-3"> {{company}} </div>
               <div class="col-5"> {{dates}} </div>
               <div class="col-3"> {{position}} </div>
            </button>
         </h2>
         <div :id="`collapse-${id}`" class="accordion-collapse collapse" :aria-labelledby="`heading-${id}`" data-bs-parent="#accordionExample" style="">
            <div class="accordion-body">
               <p> {{desc}} </p>
            </div>
         </div>
      </div>
   </div>
</template>

<style scoped>

</style>

For more information, refer to the Attribute Binding section in Vue docs

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

I am attempting to automatically refresh a mysql database through ajax without the need for a button click

My database is not updating dynamically. Is there something I am missing? It was working fine before removing some field names and automatically updating user records. But now it seems there is an issue with updating dynamically. Javascript used for form ...

Designing Games with Matrix Elements

Greetings to the StackOverflow Community! Currently, I am working on developing a JavaScript version of the well-known game Pentago as a personal side project before resuming my studies in the fall. However, I have hit a roadblock and need some guidance o ...

The Child Component in React Native Always Shows Undefined Response Status from API

In the code snippet below, I am facing an issue where the status returned by an API call is always showing as undefined in the child component, even though I can see the expected status code when logging it in the parent component. It seems like the child ...

NodeJS - session information lost after building ReactJavaScript application

I've created a NodeJS web application with basic functionality for user login and pulling data from MySql based on session information. The app is functioning as intended (Link). However, when I implement the client-side in React, the session data is ...

What is the best way to display a selected checkbox in an AJAX editing modal?

Can someone assist me in loading the checked checkboxes in an edit modal? I am using checkboxes to assign multiple roles in my system. Everything was working fine, but I encountered an issue with my edit modal where the checkboxes appear blank and unchecke ...

I just finished crafting a dynamic line chart with d3.js within a React environment. However, I am now looking to add some personalized touches to enhance its appearance. Could you kindly review the details and code

I want to create a line chart using React and D3, similar to the one depicted in this image. Presently, I have partially implemented the chart with the code provided below. You can see it here. The code snippet I've developed so far is outlined as f ...

Tips for disregarding single quotation marks in the validator.js function for checking alphanumeric characters

I am currently working on validating a string using express validator, specifically utilizing the isAlphanumeric function. However, I would like to include space, dash, and single quote as acceptable characters in the validation process. Below is the code ...

Unable to display images in the ngImgCrop upload preview modal screen

Currently, I am utilizing ngImgCrop to enable an upload preview and square crop feature for profile pictures. Unfortunately, I am experiencing issues where neither the image preview nor the cropping functionality are being displayed. 'use strict ...

Display information from a .js file onto an HTML webpage

I'm completely new to the world of server-side development and I'm attempting to navigate it on my own. Currently, I have a server written in JavaScript using Express and I'm trying to display some content on my HTML page that was sent from ...

How can the outer function be connected to the resolve method of $routeProvider?

Here is a functional code snippet: $routeProvider.when('/clients', { templateUrl:'/views/clients.html', controller:'clientsController', resolve: { rights: function ( ...

Disabling animations in Reactjs with CSSTransition and Group Transition

Currently, I am experimenting with REACTJS to build a basic app featuring Transitions. In my project file, I have imported CSSTransitions and Group Transition. However, when attempting to implement CSSTransition for specific news items, the animations are ...

Having trouble navigating through filtering in JavaScript using Node and Express?

After experimenting with different methods, I noticed that when using (age => age.Book === book && age.Chapter === 1) as my filter criteria, everything works perfectly. However, when I try using (age => age.Book === book && age.Chapt ...

In Cypress, I am trying to specifically choose only numerical values from a dropdown menu. However, the list contains a mix of alphanumeric and numeric values

Looking to streamline: This is my code: cy.get('[role=presentation]') cy.get('[role=row]').find('td') .get('[role=gridcell]').eq(9).click().wait(2000) cy.get('[role=listbox]').get('[role=option]& ...

Using Jquery to Insert Numbers Inside Brackets to Selection

I am struggling to calculate the sum of numbers in jQuery for selection fields. Currently, my code looks like this. $(function() { $("select").change(function() { updateTotal(); }); updateTotal(); }); function updateTotal() { ...

Angularjs drop-down menu changes images with animation

<body ng-controller="myCtrl"> <input type="checkbox" ng-model="loaded"/> <select ng-model="list"> <option ng-repeat="option in data.availableOptions" value="{{option.name}}">{{option.id}}</option> </select> {{list}} &l ...

Encapsulating the React Material-ui Datepicker and Timepicker OnChange event callback functionging

I'm new to React and currently incorporating Material-UI into my project for additional UI components. I've noticed some repetitive code that I'm finding difficult to refactor due to constraints within a Material-UI component's implemen ...

Updating Proxy Settings while Keeping the Driver Open in Selenium/Splinter

Previously, it was mentioned in an older version of Splinter/Selenium that changing Firefox web driver proxy settings at runtime was not possible. However, a more recent answer suggests that it can be done through JavaScript here, although I couldn't ...

Updating the state of an array containing objects within an array of objects in React

I have a state called invoices, which is an array of objects structured like this: const [invoices, setInvoices] = useState([ { id: 123, tag_number: "", item_amounts: [ { item: "processing", amount: 159 }, { i ...

Adjusting the width of the container <div> will automatically resize the inner <div> to match

I have a main container element. .wrapper{ border:1px solid blue; position:relative; top:20%; left:30%; height: 300px; width: 350px; } When I adjust the width of the parent container, the child box does not reposition itself accor ...

Check if a value is present in the array with *ngIf

I'm curious about how to use the ngIf directive in a specific scenario. In my Angular application, I have dynamically generated angular material toggles, each with a unique id. I'm familiar with using ngIf to conditionally display elements on the ...