Attribute and Event Binding

Loading concept...

Vue.js Attribute & Event Binding: The Magic Remote Control

The Story of the Magic Remote

Imagine you have a magic remote control that can change anything on your TV instantly. You press a button, and the TV responds!

In Vue.js, bindings are like that magic remote. They connect your data (what you know) to your webpage (what people see). When you change the data, the page changes automatically!


Part 1: v-bind Directive — The Connector

What is v-bind?

Think of v-bind as a magical wire that connects your data to HTML attributes.

Without v-bind: The TV remote has no batteries. Nothing happens. With v-bind: The remote works! Changes happen instantly!

Simple Example

<template>
  <img v-bind:src="pictureUrl" />
</template>

<script>
export default {
  data() {
    return {
      pictureUrl: 'cat.jpg'
    }
  }
}
</script>

What happens? The image shows whatever pictureUrl says. Change pictureUrl to 'dog.jpg', and boom — different picture!


Part 2: Attribute Binding

Making HTML Attributes Dynamic

HTML has many attributes: src, href, title, disabled, placeholder

With v-bind, you can make any of these change based on your data!

Examples

<!-- Link that goes where you tell it -->
<a v-bind:href="websiteLink">
  Visit Site
</a>

<!-- Button that can be turned off -->
<button v-bind:disabled="isLoading">
  Submit
</button>

<!-- Input with dynamic placeholder -->
<input v-bind:placeholder="hintText" />
data() {
  return {
    websiteLink: 'https://vuejs.org',
    isLoading: false,
    hintText: 'Type your name...'
  }
}

Real Life: Like a door with a smart lock. You can lock or unlock it from your phone. The door (attribute) responds to your command (data)!


Part 3: Attribute Binding Shorthand

The Quick Way: Just Use :

Writing v-bind: every time is tiring. Vue gives us a shortcut!

Long Way Short Way
v-bind:src="pic" :src="pic"
v-bind:href="link" :href="link"
v-bind:disabled="off" :disabled="off"

Example with Shorthand

<template>
  <img :src="photo" :alt="description" />
  <a :href="url">Click Me</a>
  <button :disabled="busy">Go</button>
</template>

Pro Tip: Everyone uses the shorthand. It’s cleaner and faster!


Part 4: Class Bindings

Making Elements Change Their Look

Classes control how things look (colors, sizes, animations). With Vue, you can add or remove classes dynamically!

Object Syntax — On/Off Switch

<div :class="{ active: isActive }">
  Hello!
</div>
data() {
  return {
    isActive: true
  }
}

When isActive is true: The div gets the active class. When isActive is false: The class disappears!

Multiple Classes

<div :class="{
  active: isActive,
  error: hasError,
  large: isBig
}">
  Status Box
</div>

Array Syntax — Pick from a List

<div :class="[mainClass, sizeClass]">
  Styled Box
</div>
data() {
  return {
    mainClass: 'card',
    sizeClass: 'large'
  }
}

Result: <div class="card large">

Mixing Both

<div :class="[baseClass, { highlight: isSpecial }]">
  Combo!
</div>

Part 5: Style Bindings

Inline Styles Made Dynamic

Sometimes you need to change specific CSS properties directly.

Object Syntax

<div :style="{ color: textColor, fontSize: size + 'px' }">
  Styled Text
</div>
data() {
  return {
    textColor: 'blue',
    size: 20
  }
}

Result: Blue text, 20 pixels big!

Using camelCase or kebab-case

<!-- Both work! -->
<div :style="{ backgroundColor: 'red' }">A</div>
<div :style="{ 'background-color': 'red' }">B</div>

Style Object

<div :style="boxStyles">Pretty Box</div>
data() {
  return {
    boxStyles: {
      color: 'white',
      backgroundColor: 'purple',
      padding: '20px',
      borderRadius: '10px'
    }
  }
}

Array of Styles

<div :style="[baseStyles, specialStyles]">
  Combined!
</div>

Part 6: v-on Directive — The Event Listener

Making Things Respond to Actions

Now the exciting part! What if you want something to happen when a user clicks, types, or hovers?

v-on is your answer. It listens for events and reacts!

Basic Click Example

<button v-on:click="sayHello">
  Click Me!
</button>
methods: {
  sayHello() {
    alert('Hello there!')
  }
}

What happens? Click the button → sayHello runs → Alert appears!

Common Events

Event When it Fires
click User clicks
input User types
submit Form submits
keyup Key released
mouseover Mouse hovers

Examples

<!-- Typing in input -->
<input v-on:input="updateText" />

<!-- Form submission -->
<form v-on:submit="handleSubmit">
  ...
</form>

<!-- Keyboard event -->
<input v-on:keyup="checkKey" />

Part 7: Event Binding Shorthand

The Quick Way: Just Use @

Just like : is short for v-bind:, we have @ for v-on:!

Long Way Short Way
v-on:click="go" @click="go"
v-on:input="type" @input="type"
v-on:submit="send" @submit="send"

Example with Shorthand

<template>
  <button @click="count++">
    Clicked {{ count }} times
  </button>

  <input @input="updateName" />

  <form @submit.prevent="save">
    <button type="submit">Save</button>
  </form>
</template>
data() {
  return { count: 0 }
},
methods: {
  updateName(event) {
    this.name = event.target.value
  },
  save() {
    console.log('Saved!')
  }
}

Putting It All Together

A Complete Example

<template>
  <div :class="{ dark: isDark }" :style="boxStyle">
    <h1>{{ title }}</h1>

    <img :src="imageUrl" :alt="imageDesc" />

    <button
      @click="toggleTheme"
      :disabled="isLoading"
    >
      {{ isDark ? 'Light Mode' : 'Dark Mode' }}
    </button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      title: 'My App',
      isDark: false,
      isLoading: false,
      imageUrl: 'logo.png',
      imageDesc: 'App Logo'
    }
  },
  computed: {
    boxStyle() {
      return {
        padding: '20px',
        backgroundColor: this.isDark ? '#333' : '#fff',
        color: this.isDark ? '#fff' : '#333'
      }
    }
  },
  methods: {
    toggleTheme() {
      this.isDark = !this.isDark
    }
  }
}
</script>

Quick Reference

graph TD A["Your Data"] --> B{Binding Type} B -->|Attributes| C["v-bind / :"] B -->|Events| D["v-on / @"] C --> E["src, href, disabled..."] C --> F["class binding"] C --> G["style binding"] D --> H["click, input, submit..."]

Remember This!

Task Use This
Change an attribute :attribute="data"
Add/remove classes :class="{ name: condition }"
Change inline styles :style="{ prop: value }"
Listen for events @event="handler"

You Did It!

You now understand Vue.js bindings — the magic remote control of web development!

v-bind (:) = Connect data → attributes v-on (@) = Listen for actions → respond

Go build something amazing! Your Vue journey has just begun.

Loading story...

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Stay Tuned!

Interactive content is coming soon.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Stay Tuned!

Cheatsheet is coming soon.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Stay Tuned!

Quiz is coming soon.

Flashcard Preview

Flashcard - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Stay Tuned!

Flashcards are coming soon.