BeginnerCoder

Lesson 8 — Handling events

Event handlers with arguments

For anything more than one expression, write a function in the script and call it from the template: @click="add(5)". Passing an argument lets one function serve several buttons. Inside the function, change refs through .value.

function vote(choice) { votes.value[choice]++ }
// <button @click="vote('yes')">Yes</button>

Why it matters: keeping logic in named functions makes templates readable and the logic testable.

Want it another way?
Task

Finish add(points). Clicking +5 and then +10 should show "Score: 15".

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.