Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VueJS trigger an event when a field is in focus

I have a password change page with VueJS, and I want to view the password policy only when the user clicks into the new password field. The problem is that I can't find if the page is in focus..

<b-row>
  <b-col>
    <b-form-group id="newPasswrd" label="New Password" :state="statePassword">
      <b-input-group>
        <b-form-input id="newPassword" v-model="passwords.newPassword" ref="newPassword" placeholder="Passwort" type="password" maxlength="60" />
        <p class="password-description"  >Must be at least 8 characters and contain at least two of the following: Upper case, Lower case, special characters or numbers</p>            
      </b-input-group>
    </b-form-group>
  </b-col>
</b-row>

and this is what I have in the script part of the page for testing:

      if (this.$refs.newPassword.focus() == true) console.log("focus");

My plan is to ultimately put this line in a computed and attach a bool value to it to view/hide the text below the field depending on whether if it is in focus. What happens is that I get nothing in the console when I trigger the method where this condition is written, but the focus comes to the field instead, which is not what I want. what should I do to get a bool value if the field is in focus?

like image 582
j0zeft Avatar asked Sep 03 '18 09:09

j0zeft


People also ask

How do I trigger an event at the Vue?

We can trigger events on an element with Vue. js by assigning a ref to the element we want to trigger events for. Then we can call methods on the element assigned to the ref to trigger the event.

Which event triggers when input focus is given to a form element?

The onfocus will be triggered when an element is in focus in HTML.

What is $V in Vue?

$v is an object that calls vuelidate (at the time of writing the comment, supported in version Vue. js 2.0), which is intended to check every input, which is made in a non-html form.

Why we use $emit in VUE JS?

Vue $emit is a function that lets us emit, or send, custom events from a child component to its parent. In a standard Vue flow, it is the best way to trigger certain events.


1 Answers

Bind using v-on with the native modifier:

v-on:focus.native="onFocus"

From a Core Member

Binding Native Events to Components

like image 106
Brian Lee Avatar answered Oct 19 '22 05:10

Brian Lee