Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue.js on focus textbox on load

Tags:

vue.js

vuejs2

How to focus on load using VueJS?

If using jquery, all i should do is:

$(document).ready(function() {
   $('#username').focus();
});

Is there a vue-way or not?

like image 855
Kokizzu Avatar asked Dec 23 '16 08:12

Kokizzu


1 Answers

You can create a custom directive for that:

// Register a global custom directive called v-focus
Vue.directive('focus', {
  // When the bound element is inserted into the DOM...
  inserted: function (el) {
    // Focus the element
    el.focus()
  }
})

And then use it like this:

<input v-focus>

Full example from the docs: https://v2.vuejs.org/v2/guide/custom-directive.html

Directive docs: https://v2.vuejs.org/v2/api/#Vue-directive

like image 130
sobolevn Avatar answered Nov 13 '22 22:11

sobolevn