by Sai gowtham

How to access the dom nodes in Vue using refs

In this tutorial, we are going to learn about accessing the dom nodes in Vue using the refs.

Refs

Refs are used to access the html elements inside the vue instance.

If you add a ref attribute to the HTML element in your vue template, that element can be accessed in your vue instance using this.$refs property.

Let’s see an example.

<template>
  <div>
     <label for="box">Search : </label>
    <input ref="search" v-model="query" id="box" />  </div>
</template>

<script>
export default {
  data: function() {
    return {
      query: ""
    };
  },
  mounted: function() {
    // accessing the input node
    this.$refs["search"].focus();  }
};
</script>

In the above code, we have added ref atrribute to input element so that we accessed it inside the mounted lifecycle hook with this.$refs['search'].

Whenever a user opens our app we are focussing the input placeholder like Google search box.

vue-refs-input-element

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2021 (Beginner + Advanced)
JavaScript - The Complete Guide 2021 (Beginner + Advanced)
64,026 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
152,857 students enrolled
48.5 hours of video content
View Course