0
0
Vueframework~20 mins

Dynamic attribute names in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dynamic Attribute Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What attribute does this Vue component render dynamically?

Consider this Vue 3 component using the Composition API. What attribute will the rendered <input> element have?

Vue
<template>
  <input v-bind:[dynamicAttr]="inputValue" />
</template>

<script setup>
import { ref } from 'vue'
const dynamicAttr = 'placeholder'
const inputValue = ref('Enter your name')
</script>
AAn input with a placeholder attribute set to 'Enter your name'
BAn input with a value attribute set to 'Enter your name'
CAn input with a dynamicAttr attribute set to 'Enter your name'
DAn input with no attributes set
Attempts:
2 left
💡 Hint

Look at how the attribute name is set using square brackets inside the binding.

📝 Syntax
intermediate
2:00remaining
Which option correctly binds a dynamic attribute in Vue 3?

Which of the following Vue template bindings correctly sets a dynamic attribute name stored in attrName with value attrValue?

A<input v-bind:attrName="attrValue" />
B<input :attrName="attrValue" />
C<input :[attrName]="attrValue" />
D<input v-bind:[attrName]="attrValue" />
Attempts:
2 left
💡 Hint

Remember that dynamic attribute names require square brackets inside the binding directive.

🔧 Debug
advanced
2:00remaining
Why does this dynamic attribute binding not work as expected?

Given this Vue 3 component snippet, why does the dynamic attribute not appear on the <button> element?

Vue
<template>
  <button :[attr]="true">Click me</button>
</template>

<script setup>
const attr = null
</script>
ABecause the value true is invalid for any attribute
BBecause the button element does not support dynamic attributes
CBecause <code>attr</code> is null, Vue cannot set a dynamic attribute with a null name
DBecause dynamic attributes require the value to be a string
Attempts:
2 left
💡 Hint

Check the value of the variable used for the attribute name.

state_output
advanced
2:00remaining
What is the rendered output after clicking the button?

In this Vue 3 component, clicking the button toggles the dynamic attribute name. What attribute does the button have after the first click?

Vue
<template>
  <button :[attrName]="true" @click="toggle">Toggle</button>
</template>

<script setup>
import { ref } from 'vue'
const attrName = ref('disabled')
function toggle() {
  attrName.value = attrName.value === 'disabled' ? 'aria-pressed' : 'disabled'
}
</script>
AThe button has the attribute disabled="true"
BThe button has the attribute aria-pressed="true"
CThe button has both disabled and aria-pressed attributes
DThe button has no attributes
Attempts:
2 left
💡 Hint

Think about how the attribute name changes after the click.

🧠 Conceptual
expert
2:00remaining
Why use dynamic attribute names in Vue components?

Which of the following best explains a practical reason to use dynamic attribute names in Vue?

ATo conditionally bind different HTML attributes based on component state or props without repeating code
BTo enable two-way data binding on any attribute automatically
CTo improve performance by avoiding re-rendering of static attributes
DTo allow Vue to generate unique attribute names for CSS styling
Attempts:
2 left
💡 Hint

Think about why you might want to change attribute names dynamically instead of hardcoding them.