You now have reactive state in Colmena Bookings, but you only display it as text with {{ }}. A real interface needs much more: a card's image that depends on the room, a link that points to the plan of the right floor, a button that is disabled when the room is unavailable, or a screen reader that announces the state of a control. All of these are attributes, and connecting them to the state is the job of v-bind. In this lesson you will study one-way binding (from the state to the DOM) in depth and take your first step with the two-way binding of v-model, understanding what it really does under the hood. We will apply it by adding an image and a link to RoomCard, disabling buttons according to availability and creating a room search box.

Contents

  1. One-way data flow
  2. v-bind on everyday attributes: src, href, alt and title
  3. aria-* accessibility attributes
  4. Boolean attributes and the role of null and undefined
  5. Binding several attributes with v-bind="object"
  6. The same-name shorthand
  7. DOM properties versus HTML attributes
  8. A first look at v-model: two-way binding
  9. What v-model does under the hood
  10. One-way versus two-way
  11. Case study: image, link, buttons and search box in Colmena

  1. One-way data flow

In the Template Syntax lesson you saw that v-bind (shortened to :) evaluates a JavaScript expression and uses the result as the value of an attribute. The essential characteristic of this binding is its direction: data flows from the state to the DOM, never the other way round.

flowchart LR
    E["Reactive state (script)"] -->|"v-bind / {{ }}"| D["DOM attributes and text"]

If the state changes, the attribute is updated. But if something changes the attribute in the DOM (for example, an external script or the user typing in a field), the state does not find out. This single direction makes the interface's behaviour predictable: to know what is on screen, you only need to look at the state.

  1. v-bind on everyday attributes: src, href, alt and title

The attributes you will bind most often are the ones that identify resources and describe elements:

<script setup>
const room = { id: 'panal', name: 'Panal Room', floor: 1 }
</script>

<template>
  <img :src="`/img/rooms/${room.id}.webp`" :alt="`Photo of the ${room.name}`" />

  <a :href="`/floor-plans/floor-${room.floor}.pdf`" target="_blank" rel="noopener">
    View the floor {{ room.floor }} plan
  </a>

  <h2 :title="`Internal ID: ${room.id}`">{{ room.name }}</h2>
</template>

Result in the DOM:

<img src="/img/rooms/panal.webp" alt="Photo of the Panal Room">
<a href="/floor-plans/floor-1.pdf" target="_blank" rel="noopener">View the floor 1 plan</a>
<h2 title="Internal ID: panal">Panal Room</h2>

Notice several things:

  • You can mix bound attributes (:href) and static ones (target="_blank") on the same element. Static attributes have no colon because their value is fixed text.
  • Paths starting with / point to the project's public/ folder: /img/rooms/panal.webp is the file public/img/rooms/panal.webp, which Vite serves as it is.
  • rel="noopener" on links with target="_blank" prevents the opened page from accessing yours: it is good security practice.
  • The alternative text (alt) is essential for accessibility: screen readers read it out and it is displayed if the image does not load. Binding it to the room name means it is always descriptive.

  1. aria-* accessibility attributes

ARIA (Accessible Rich Internet Applications) attributes tell assistive technologies, such as screen readers, things that HTML alone does not express. Because they usually depend on the state, they are bound just like any other attribute:

<script setup>
import { ref } from 'vue'

const booked = ref(3)
const capacity = 8
</script>

<template>
  <p
    role="status"
    :aria-label="`${booked} of ${capacity} seats booked`"
  >
    {{ booked }}/{{ capacity }}
  </p>

  <button :aria-pressed="booked > 0 ? 'true' : 'false'">My bookings</button>
</template>
  • :aria-label: a screen reader would read out "3 of 8 seats booked" instead of the cryptic "3/8".
  • role="status": indicates that the content is a status message; when it changes, some screen readers announce it.
  • :aria-pressed: indicates whether a toggle button is switched on. ARIA attributes expect the strings 'true' and 'false', not the absence of the attribute, hence the ternary.

  1. Boolean attributes and the role of null and undefined

4.1 Boolean attributes

In HTML, some attributes are boolean: their mere presence switches the behaviour on, whatever their value. disabled, readonly, required, checked, hidden and multiple are examples. <button disabled="false"> is still disabled! That is why Vue treats them in a special way:

<script setup>
const room = { name: 'Enjambre Auditorium', available: false }
</script>

<template>
  <button :disabled="!room.available">Book</button>
  <!-- available = false → <button disabled="">Book</button> -->
  <!-- available = true  → <button>Book</button> (the attribute disappears) -->
</template>

With a boolean attribute, Vue includes it if the value is truthy (or the empty string '') and removes it if it is falsy. This is exactly what you need to disable controls according to the state.

4.2 null and undefined remove any attribute

For all other attributes (the non-boolean ones), the rule is different and worth memorising:

Bound value Boolean attribute (disabled) Normal attribute (title, aria-*, data-*...)
true Present: disabled="" title="true"
false Removed title="false" (kept as text)
'' (empty string) Present title=""
null or undefined Removed Removed
Any other value Present Converted to text

In other words, to remove a normal attribute, use null or undefined, not false. A typical use is showing a tooltip only under certain conditions:

<button
  :disabled="!room.available"
  :title="room.available ? null : 'This room is not available right now'"
>
  Book
</button>

If the room is available, the button has no title; if not, the user sees the reason when hovering over it.

In Vue 2, false also removed normal attributes. If you are migrating old code, check this change: an :aria-hidden="false" now produces aria-hidden="false", which is correct for ARIA.

  1. Binding several attributes with v-bind="object"

If an element needs several attributes that you already have grouped in an object, you can bind them all at once by writing v-bind without an argument:

<script setup>
const room = { id: 'panal', name: 'Panal Room' }

const imageAttributes = {
  src: `/img/rooms/${room.id}.webp`,
  alt: `Photo of the ${room.name}`,
  width: 320,
  height: 180,
  loading: 'lazy'
}
</script>

<template>
  <img v-bind="imageAttributes" class="photo" />
  <!-- <img src="/img/rooms/panal.webp" alt="Photo of the Panal Room"
            width="320" height="180" loading="lazy" class="photo"> -->
</template>
  • Each property of the object becomes an attribute with its name and value.
  • It can be combined with individual attributes. If the same attribute appears both in the object and on its own, whichever is written later in the template wins: in <img v-bind="imageAttributes" alt="Another">, the final alt is "Another"; in <img alt="Another" v-bind="imageAttributes">, it is the object's.
  • width and height reserve the image's space before it loads (preventing the page from jumping) and loading="lazy" delays its download until it is close to the visible area.

This pattern is very useful when a component receives attributes from outside and passes them on, something you will see in Template Refs and Fallthrough Attributes.

  1. The same-name shorthand

Since Vue 3.4, if the attribute and the variable you give it as its value have the same name, you can omit the value:

<script setup>
const placeholder = 'Search by room name'
const maxlength = 40
</script>

<template>
  <!-- Full form -->
  <input :placeholder="placeholder" :maxlength="maxlength" />

  <!-- Same-name shorthand: identical to the previous one -->
  <input :placeholder :maxlength />
</template>

It works with kebab-case names: :aria-label would look for a variable called ariaLabel. It is a convenience, not an obligation; use it when the variable name really is the attribute's name.

  1. DOM properties versus HTML attributes

There is a subtlety that crops up sooner or later: a DOM element has attributes (what is written in the HTML, always text) and properties (what the element's JavaScript object holds, of any type). They often coincide, but not always:

Concept HTML attribute DOM property
What it is Text in the markup: <input value="Panal"> A field of the object: input.value
Type Always a string Any (string, number, boolean, object)
On an <input> value is the initial value value is the current value (what the user has typed)
Read with element.getAttribute('value') element.value

When you write :something="...", Vue decides automatically: if the element has a property with that name (such as value on an <input>), it sets the property; otherwise, it sets the attribute. In practice it almost always gets it right. For the rare cases (above all with Web Components), you can force the behaviour with modifiers:

  • .prop: forces the DOM property. :textContent.prop="message" sets element.textContent (write the property name as it is in JavaScript, in camelCase). It is shortened with a leading dot: .textContent="message".
  • .attr: forces the HTML attribute. :initial-value.attr="x". It is shortened with ^: ^initial-value="x".

You will not need them in Colmena; it is enough to know that they exist and why.

  1. A first look at v-model: two-way binding

There are elements where the user modifies the value: form fields. For a search box you need the typed text to reach the state. With v-bind alone, that does not happen:

<script setup>
import { ref } from 'vue'

const search = ref('')
</script>

<template>
  <input :value="search" />
  <p>Searching for: "{{ search }}"</p>
  <!-- You type in the field... and the paragraph stays empty: the data has not changed -->
</template>

:value takes the state to the field, but what the user types stays in the DOM. You also need the way back. That is what v-model is for, the two-way binding directive:

<template>
  <input v-model="search" />
  <p>Searching for: "{{ search }}"</p>
  <!-- Now the paragraph changes with every keystroke -->
</template>

With v-model:

  • What the user types is stored in search (DOM → state).
  • If the code changes search (for example, search.value = '' to clear it), the field is updated (state → DOM).
  • The ref's value is the single source of truth: v-model ignores any initial value attribute you may have written on the <input>. The initial value is given in the script (ref('')).

In this lesson we limit ourselves to a text <input>. The other controls (checkboxes, radio buttons, drop-down lists, text areas), the v-model modifiers and complete forms are covered in Forms with v-model.

  1. What v-model does under the hood

v-model is not magic: it is syntactic sugar that the compiler expands into two pieces you already know, a binding and an event. On a text <input>:

<!-- This... -->
<input v-model="search" />

<!-- ...is essentially equivalent to this: -->
<input :value="search" @input="search = $event.target.value" />
  • :value="search": the way there, from the state to the field (one-way, like all v-bind).
  • @input="...": the way back. The input event fires on every keystroke; $event.target is the <input> itself, and $event.target.value is its current text. Events and $event are covered in the next lesson.
sequenceDiagram
    participant U as User
    participant I as input (DOM)
    participant E as search (state)
    U->>I: types "nec"
    I->>E: input event → search = 'nec'
    E->>I: :value = 'nec' (already matches)
    E->>E: the effects that use search are updated
    Note over E: The code does search.value = ''
    E->>I: :value = '' (the field is emptied)

Knowing this has practical uses: if you ever need to do something extra when the user types (for example, converting to lower case), you can use the expanded form. What is more, the real v-model adds details that the manual version lacks, such as waiting for character composition to finish in languages with special input methods (Chinese, Japanese...).

  1. One-way versus two-way

Aspect One-way (v-bind, {{ }}) Two-way (v-model)
Direction State → DOM State ↔ DOM
Where it is used Any attribute or text Form fields (and your own components, in 03-03)
Who changes the value Only your code Your code and the user
Equivalent — :value + @input (on text)
Predictability Maximum: the state is in charge High: the state is still the source of truth
flowchart TB
    subgraph U["One-way"]
        E1["selectedRoom (state)"] -->|":disabled, :src, :href"| D1["DOM attributes"]
    end
    subgraph B["Two-way"]
        E2["search (state)"] -->|":value"| D2["input"]
        D2 -->|"@input"| E2
    end

Rule of thumb: everything is one-way except the controls where the user types or chooses. Even with v-model, think of it as "one-way plus an event that updates the state": that way you will never lose sight of what the source of truth is.

  1. Case study: image, link, buttons and search box in Colmena

11.1 Image and floor plan in RoomCard.vue

Create the public/img/rooms/ and public/floor-plans/ folders in the project. In the first, put any image named panal.webp (you can convert any photo to WebP with an image editor or an online service) and in the second, any PDF named floor-1.pdf. If you do not, the image will display its alternative text, which also lets you check that the alt works.

Modify src/components/RoomCard.vue. In the script, after getting the room, add the image attributes:

// --- Presentation ---
const imageAttributes = {
  src: `/img/rooms/${room.id}.webp`,
  alt: `Photo of the ${room.name}`,
  width: 320,
  height: 180,
  loading: 'lazy'
}
const floorPlanUrl = `/floor-plans/floor-${room.floor}.pdf`

And in the template:

<template>
  <article class="card" :id="'room-' + room.id" :title="`${room.name}: floor ${room.floor}`">
    <img v-bind="imageAttributes" class="photo" />
    <h2>{{ room.name }}</h2>
    <p>Floor {{ room.floor }} · {{ formatPrice(room.hourlyPrice) }}/h</p>
    <p>Equipment: {{ room.equipment.join(', ') }}</p>
    <p>{{ room.available ? 'Available' : 'Unavailable' }}</p>
    <p>Attendees: {{ attendees }} / {{ room.capacity }}</p>
    <p>Free seats: <strong>{{ freeSeats }}</strong></p>
    <p class="notice">{{ notice }}</p>
    <button :disabled="attendees === 0" @click="removeAttendee">− Attendee</button>
    <button
      :disabled="!room.available || full"
      :title="room.available ? null : 'Room unavailable'"
      @click="addAttendee"
    >
      + Attendee
    </button>
    <a :href="floorPlanUrl" target="_blank" rel="noopener">Floor {{ room.floor }} plan</a>
  </article>
</template>

<!-- Add to <style scoped>: -->
<style scoped>
.photo {
  display: block;
  width: 100%;
  height: auto;
  border-radius: 6px;
  background: var(--color-background); /* visible while loading or if it fails */
}
</style>
  • v-bind="imageAttributes": the image's five attributes in a single line.
  • :disabled="attendees === 0" on "− Attendee": there is no point clicking it if nobody is there.
  • :disabled="!room.available || full" on "+ Attendee": it is disabled if the room is unavailable or already full. Notice that full is a computed property: in the template it is used without .value.
  • :title with null: the tooltip only appears when the room is unavailable.

To check the "unavailable" case, temporarily change rooms.find((r) => r.id === 'panal') in the script to 'enjambre': the button will appear disabled with its explanation. Then change it back to 'panal'.

The addAttendee and removeAttendee functions already checked the limits. Disabling the button does not make them unnecessary: the button communicates the state to the user and the function protects the logic. Keep both lines of defence.

11.2 Disabled button and search box in App.vue

In src/App.vue, add the search box state and a function that says whether a room matches it:

// --- Search ---
const search = ref('')
const placeholder = 'Search by name, e.g. "néctar"'

function matchesSearch(room) {
  const text = search.value.trim().toLowerCase()
  return room.name.toLowerCase().includes(text)
}

And update the <main> template:

<main class="content">
  <!-- ... the note, counter and buttons from before ... -->

  <div class="search-box">
    <label for="search">Search rooms</label>
    <input id="search" v-model="search" type="search" :placeholder />
    <p>
      Searching for: "{{ search }}" ·
      {{ matchesSearch(selectedRoom) ? 'the selected room matches' : 'the selected room does not match' }}
    </p>
  </div>

  <section class="selection">
    <h2>Selected room: {{ selectedRoom.name }}</h2>
    <p>Floor {{ selectedRoom.floor }} · up to {{ selectedRoom.capacity }} people</p>
    <p>Booked seats: {{ seats.booked }} / {{ selectedRoom.capacity }}</p>
    <button
      class="button"
      :disabled="!selectedRoom.available || seats.booked >= selectedRoom.capacity"
      :title="selectedRoom.available ? null : 'Room under maintenance'"
      @click="bookSeat"
    >
      Book a seat
    </button>
    <button class="button" @click="nextRoom">Next room</button>
  </section>

  <RoomCard />
</main>

Then add a style for disabled buttons and the search box to the <style scoped> of App.vue:

.button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.search-box input {
  margin-left: 0.5rem;
  padding: 0.4rem 0.6rem;
}

What you have achieved:

  • <label for="search"> + id="search": the label text is associated with the field; clicking it gives the field focus, and screen readers announce it correctly.
  • v-model="search": every keystroke updates search and, with it, the paragraph below.
  • :placeholder: the same-name shorthand.
  • matchesSearch(selectedRoom): a pure function called from the template. trim() ignores spaces at the beginning and end, and toLowerCase() makes the comparison case-insensitive. Type "room" and click "Next room" until you reach the Enjambre Auditorium: it will stop matching.
  • The "Book a seat" button is disabled when you reach the auditorium (under maintenance) and when the room is full.

You will have noticed that the search only half ignores accents: "nectar" does not find "Néctar". We will solve this in exercise 3. And for now the search box only compares against the selected room: filtering the full list of rooms will come in List Rendering.

Common Mistakes and Tips

  • Using false to remove a normal attribute. :title="false" produces title="false". Use null or undefined.
  • Writing disabled="false" without the colon. It is a static attribute with the value "false": the button ends up disabled. If it depends on the state, use :disabled="...".
  • Relying only on :disabled to protect the logic. A disabled button can be re-enabled from the browser's developer tools. The function must still check the conditions.
  • Expecting :value to update the state. It is one-way. For editable fields, use v-model.
  • Giving the initial value in the HTML with v-model. <input v-model="search" value="panal"> ignores value. The initial value is given in the script: ref('panal').
  • Forgetting the alt on images or leaving it generic ("image"). Bind it to data that describes the content.
  • Tip: when a template has many attributes calculated with long template literals, move them into variables in the script (floorPlanUrl, imageAttributes): the template will be easier to read and you will be able to reuse them.

Exercises

Exercise 1. In the "Selected room" section of App.vue, add the image of the selected room (with the same path convention as in the card) and a link to its floor plan. Both must change when "Next room" is clicked. Use individual attributes, not v-bind="object". Why can you not define an imageAttributes object here, as in RoomCard, and expect it to update?

Exercise 2. Add a "Clear" button to the search box in App.vue that empties the search and is disabled when nothing has been typed. Check that clicking it also empties the text field. Which direction of the binding makes this possible?

Exercise 3. Improve matchesSearch so that it ignores accents and "nectar" finds "Néctar Room". Hint: text.normalize('NFD') separates letters from their accents, and .replace(/[̀-ͯ]/g, '') removes the separated accents.

Solutions

Solution 1.

<section class="selection">
  <img
    :src="`/img/rooms/${selectedRoom.id}.webp`"
    :alt="`Photo of the ${selectedRoom.name}`"
    width="320"
    height="180"
    class="photo"
  />
  <h2>Selected room: {{ selectedRoom.name }}</h2>
  <!-- ... -->
  <a :href="`/floor-plans/floor-${selectedRoom.floor}.pdf`" target="_blank" rel="noopener">
    Floor {{ selectedRoom.floor }} plan
  </a>
</section>

An object such as const imageAttributes = { src: `/img/rooms/${selectedRoom.value.id}.webp`, ... } would be calculated only once, when the script runs, with the initial room. It is an ordinary object: it does not know that it depends on selectedRoom. Template expressions, on the other hand, are re-evaluated on every render. In RoomCard it worked because the room never changes. The way to have an object that recalculates itself is a computed property, which you will see in 02-05.

Solution 2.

function clearSearch() {
  search.value = ''
}
<button class="button" :disabled="search === ''" @click="clearSearch">Clear</button>

The field is emptied thanks to the state → DOM part of v-model (its internal :value): when search is changed from the code, the <input> is updated. With only whitespace typed, the button would still be enabled; if you would rather it were not, use :disabled="search.trim() === ''".

Solution 3.

function normalize(text) {
  return text
    .normalize('NFD')                 // "é" becomes "e" + a combining accent
    .replace(/[̀-ͯ]/g, '')  // removes the combining accents
    .trim()
    .toLowerCase()
}

function matchesSearch(room) {
  return normalize(room.name).includes(normalize(search.value))
}

Now "nectar", "NÉCTAR" or " néctar " find the Néctar Room. Extracting normalize into its own function means we apply it in exactly the same way to the search text and to the room name, which is precisely what guarantees a consistent comparison.

Conclusion

v-bind connects the state to the DOM's attributes in a single direction: from the image and its alt to links, ARIA attributes and boolean attributes such as disabled, which Vue adds or removes according to the value. Remember that null and undefined remove any attribute whereas false only removes boolean ones, that v-bind="object" binds several attributes in one go and that, since Vue 3.4, :placeholder is enough if the variable has the same name. Vue chooses between DOM property and attribute, and you can force it with .prop and .attr. Finally, v-model adds the way back in form fields: it is :value plus an input event. Colmena Bookings now shows the room's photo and floor plan, disables buttons when there is no point clicking them and has a first search box.

There is an event hidden inside v-model, and the application already has several @clicks. In the next lesson, Event Handling, we will study v-on in detail: how to access the native event, pass arguments and use modifiers so that the search box filters when Enter is pressed and clears with Escape.

New edition · October 2026

Vue.js Course

Module 1: Introduction to Vue.js

Module 2: Vue.js Basics

Module 3: Vue.js Components

Module 4: Vue Router

Module 5: State Management with Vuex and Pinia

Module 6: Vue.js Directives

Module 7: Vue.js Plugins

Module 8: Testing in Vue.js

Module 9: Advanced Vue.js Concepts

Module 10: Building and Deploying Vue.js Applications

Module 11: Real-World Vue.js Projects

© Copyright 2026. All rights reserved