Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Vue Tutorial

Vue HOME Vue Intro Vue Directives Vue v-bind Vue v-if Vue v-show Vue v-for Vue Events Vue v-on Vue Methods Vue Event Modifiers Vue Forms Vue v-model Vue CSS Binding Vue Computed Properties Vue Watchers Vue Templates

Scaling Up

Vue Why, How and Setup Vue First SFC Page Vue Components Vue Props Vue v-for Components Vue $emit() Vue Fallthrough Attributes Vue Scoped Styling Vue Local Components Vue Slots Vue v-slot Vue Scoped Slots Vue Dynamic Components Vue Teleport Vue HTTP Request Vue Template Refs Vue Lifecycle Hooks Vue Provide/Inject Vue Routing Vue Form Inputs Vue Animations Vue Animations with v-for Vue Build Vue Composition API

Vue Reference

Vue Built-in Attributes Vue Built-in Components Vue Built-in Elements Vue Component Instance Vue Directives Vue Instance Options Vue Lifecycle Hooks

Vue Examples

Vue Examples Vue Exercises Vue Quiz Vue Server Vue Certificate

Vue $el Object


Example

Using the $el object to change the background color of a <div> tag on root level.

methods: {
  changeColor() {
    this.$el.style.backgroundColor = 'lightgreen';
  }
}
Run Example »

See more examples below.


Definition and Usage

The $el object represents the root DOM node of the Vue component.

The $el object does not exist until the Vue application is mounted.

If there is only one HTML root element in the <template>:

  • the $el object will be that root element.
  • the DOM can be manipulated directly using the $el object (see the example above), but it is not recommended.
  • it is better to use the Vue ref attribute and other Vue functionality to change the DOM declaratively, because it leads to code that is more consistent and easier to maintain (see Example 1 below).

If there is more than one HTML root element in the <template>:

  • the $el object will just be a placeholder DOM text node that Vue uses internally (not the actual DOM element).
  • the DOM cannot be manipulated using the $el object when there are multiple root elements (see Example 2 below).

Note: In Vue 3's Composition API, the $el property is not accessible in <script setup> (using the setup function).


More Examples

Example 1

Using the ref attribute to change the background color of a <div> tag declaratively as recommended, instead of using the $el object.

<template>
  <div ref="rootDiv">
    <h2>Example $el Object</h2>
    <p>It is recommended, and more consistent, to use the ref attribute instead of the $el object to change the background color root DIV tag.</p>
    <button v-on:click="changeColor">Click here</button>
  </div>
</template>

<script>
export default {
  methods: {
    changeColor() {
      this.$refs.rootDiv.style.backgroundColor = 'lightgreen';
    }
  }
}
</script>
Run Example »

Example 2

With more than one element in the root of the <template>, the $el object will just be a text node representation (not the actual DOM element) of the first element of the root elements, used internally by Vue.

We cannot manipulate the DOM with the $el object is such cases.

<template>
  <div>
    <h2>Example $el Object</h2>
    <p>We are not able to use the $el object to change the background color of the root DIV tag when there are other tags on the root level. Open browser console to see the error generated.</p>
    <button v-on:click="changeColor">Click here</button>
  </div>
  <p>With this extra p-tag there are two tags on the root level.</p>
</template>

<script>
export default {
  methods: {
    changeColor() {
      this.$el.style.backgroundColor = 'lightgreen';
    }
  }
}
</script>

<style>
#app > div, #app > p {
  border: solid black 1px;
  padding: 10px;
}
</style>
Run Example »

Example 3

Using the $el object to change the background color of a <h2> child element.

<template>
  <div>
    <h2>Example $el Object</h2>
    <p>Using the $el object to change the background color of the H2 child element.</p>
    <button v-on:click="changeColor">Click here</button>
  </div>
</template>

<script>
export default {
  methods: {
    changeColor() {
      this.$el.children[0].style.backgroundColor = 'lightblue';
    }
  }
}
</script>
Run Example »

Related Pages

Vue Tutorial: Vue Template Refs

Vue Tutorial: Vue Methods

Vue Reference: Vue 'ref' Attribute

Vue Reference: Vue $refs Object