0
0
Vueframework~10 mins

Dynamic attribute names in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Dynamic attribute names
Start Vue Component Setup
Define reactive data
Use dynamic attribute binding
Vue template compiles
Attribute name resolved from data
Attribute applied to element
Rendered output updates
User sees element with dynamic attribute
Vue reads the attribute name from reactive data and applies it dynamically to the element during rendering.
Execution Sample
Vue
<template>
  <input :[attrName]="value" />
</template>

<script setup>
import { ref } from 'vue'
const attrName = ref('placeholder')
const value = ref('Enter text')
</script>
This Vue component binds an input attribute dynamically using the value of attrName.
Execution Table
StepActionattrName.valuevalue.valueDynamic Attribute AppliedRendered Output
1Component setup startsplaceholderEnter textnone yet<input> (not rendered)
2Vue compiles templateplaceholderEnter textplaceholder<input placeholder="Enter text">
3Reactive data changes attrName to 'title'titleEnter texttitle<input title="Enter text">
4Reactive data changes value to 'Type here'titleType heretitle<input title="Type here">
5attrName changes back to 'aria-label'aria-labelType herearia-label<input aria-label="Type here">
6value changes to 'Accessible input'aria-labelAccessible inputaria-label<input aria-label="Accessible input">
7Component unmountsaria-labelAccessible inputnone(component removed)
💡 Component unmounts, stopping reactive updates and rendering.
Variable Tracker
VariableStartAfter 2After 3After 4After 5After 6Final
attrNameplaceholderplaceholdertitletitlearia-labelaria-labelaria-label
valueEnter textEnter textEnter textType hereType hereAccessible inputAccessible input
Key Moments - 3 Insights
Why does the attribute name change on the element when attrName changes?
Because Vue tracks reactive variables like attrName and updates the DOM attribute dynamically as shown in execution_table rows 3 and 5.
What happens if attrName changes to a name that is not a valid HTML attribute?
Vue still applies it as an attribute, but it may not affect the element visually or functionally. The execution_table shows attribute names changing dynamically regardless of validity.
Why do we use square brackets around attrName in :[attrName]?
The square brackets tell Vue to treat attrName as a dynamic key, not a literal attribute name. This is why the attribute name changes as attrName changes, as seen in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What attribute is applied to the input element?
Aplaceholder
Baria-label
Ctitle
Dvalue
💡 Hint
Check the 'Dynamic Attribute Applied' column at step 3 in the execution_table.
At which step does the input element get the attribute 'aria-label'?
AStep 5
BStep 4
CStep 2
DStep 6
💡 Hint
Look for when attrName.value changes to 'aria-label' in the execution_table.
If value changes to 'Hello' at step 4 instead of 'Type here', what will be the rendered attribute value?
A"Type here"
B"Hello"
C"Enter text"
D"Accessible input"
💡 Hint
Check the 'value.value' column at step 4 in the variable_tracker and execution_table.
Concept Snapshot
Dynamic attribute names in Vue:
Use :[attrName]="value" syntax.
attrName is reactive and controls the attribute key.
Value sets the attribute's value.
Vue updates the DOM attribute when attrName or value changes.
Useful for flexible, data-driven attributes.
Full Transcript
This visual execution shows how Vue handles dynamic attribute names using the syntax :[attrName]. The component defines reactive variables attrName and value. Initially, attrName is 'placeholder' and value is 'Enter text'. Vue compiles the template and applies the attribute placeholder="Enter text" to the input element. When attrName changes to 'title', Vue updates the attribute to title="Enter text". Changing value updates the attribute's value accordingly. Later, attrName changes to 'aria-label' and value updates to 'Accessible input', and Vue reflects these changes in the rendered input element. The execution table tracks each step, showing how reactive data drives dynamic attribute binding. This helps beginners see how Vue's reactivity updates the DOM attributes dynamically without manual DOM manipulation.