0
0
Vueframework~20 mins

v-on directive for events in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Event Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when clicking the button?
Consider this Vue component using v-on to handle a click event. What will be the displayed output after clicking the button once?
Vue
<template>
  <button v-on:click="increment">Click me</button>
  <p>Count: {{ count }}</p>
</template>

<script setup>
import { ref } from 'vue'
const count = ref(0)
function increment() {
  count.value++
}
</script>
AThe paragraph shows 'Count: NaN' after clicking.
BThe paragraph shows 'Count: 0' even after clicking.
CThe paragraph shows 'Count: 1' after one click.
DThe button click causes a runtime error.
Attempts:
2 left
💡 Hint
Remember that v-on:click calls the increment function which increases the count.
📝 Syntax
intermediate
1:30remaining
Which option correctly binds a click event using v-on shorthand?
In Vue, which of these options correctly uses the shorthand for v-on:click to call the submitForm method?
A<button v-on-click="submitForm">Submit</button>
B<button v-on:click()="submitForm">Submit</button>
C<button v-on:click=>"submitForm">Submit</button>
D<button @click="submitForm">Submit</button>
Attempts:
2 left
💡 Hint
The shorthand for v-on:click is @click.
🔧 Debug
advanced
2:30remaining
Why does this event handler not work as expected?
Given this Vue component, clicking the button does not update the message. What is the cause?
Vue
<template>
  <button v-on:click="changeMessage">Change</button>
  <p>{{ message }}</p>
</template>

<script setup>
import { ref } from 'vue'
const message = ref('Hello')
function changeMessage() {
  message.value = 'Goodbye'
}
</script>
AThe 'changeMessage' function is not defined properly.
BThe variable 'message' is not reactive, so changes don't update the template.
CThe <code>v-on:click</code> directive is misspelled.
DThe template syntax for displaying 'message' is incorrect.
Attempts:
2 left
💡 Hint
Check if the variable used in the template is reactive.
state_output
advanced
2:00remaining
What is the value of count after clicking the button twice?
Analyze this Vue component. What will be the value of count after clicking the button two times?
Vue
<template>
  <button v-on:click="increment">Add</button>
  <p>{{ count }}</p>
</template>

<script setup>
import { ref } from 'vue'
const count = ref(0)
function increment() {
  count.value += 2
}
</script>
A4
B1
C2
D0
Attempts:
2 left
💡 Hint
Each click adds 2 to the count.
🧠 Conceptual
expert
3:00remaining
Which option correctly prevents the default form submission using v-on?
You want to prevent the default form submission when clicking the submit button in Vue. Which option correctly uses v-on modifiers to do this?
Vue
<template>
  <form @submit.prevent="handleSubmit">
    <button type="submit">Submit</button>
  </form>
</template>

<script setup>
function handleSubmit() {
  alert('Form submitted')
}
</script>
A<form @submit.prevent="handleSubmit">...</form>
B<form v-on:submit.stop="handleSubmit">...</form>
C<form v-on:submit.capture="handleSubmit">...</form>
D<form @submit.once="handleSubmit">...</form>
Attempts:
2 left
💡 Hint
Use the modifier that stops the default browser action.