Set Focus for component in VueJS

ASD

This is my code.

import Datepicker from 'vuejs-datepicker'

if(this.DOB > this.dateOfJoin){
  alert("DOB should not be greater than Date of Join.asd.!")
  this.$refs.dob.focus();
}
<div class="form-group">
  <label class="col-sm-3 control-label">Date of Birth</label>
  <datepicker v-model="DOB" name="DOB"  ref="dob" class="required"></datepicker>
</div>

I am using the vuejs-datepicker component, but I cannot set focus on the datepicker.

Bert

After reading through the docs, I recommend a different approach. The datepicker has a disabled property. This allows you to specify a from property that the date cannot be greater than and a to property that the date cannot be earlier than. So if you do not want users to select a date of birth greater than the date they joined, set the from property to the date joined.

In data properties

data:{
    dobDisabled:{ from: <date joined> }
}

And on the datepicker,

<datepicker :disabled="dobDisabled"></datepicker>

That will prevent users from picking an invalid date.

Here is an example.

Original

It looks like the datepicker you are using renders a readonly input element, which I expect cannot get focus. In any case, the component which wraps the input does not support a focus method.

  <input
    :type="inline ? 'hidden' : 'text'"
    :class="[ inputClass, { 'form-control' : bootstrapStyling } ]"
    :name="name"
    :id="id"
    @click="showCalendar"
    :value="formattedValue"
    :placeholder="placeholder"
    :clear-button="clearButton"
    :disabled="disabledPicker"
    :required="required"
    readonly>

Instead you might try showing the calendar.

this.$refs.dob.showCalendar()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Set focus on a component with Apache Wicket?

Angular 2 Set focus on first field of form on component load

Set focus to element of nested component in Angular 4

Call .focus() on a sibling VueJS Component

how to set focus for AutoComplete component from PrimeNG library in Angular

How to set data to this.$root when testing a VueJS component?

In Inno Setup, how can I set which component gets an initial focus on "Select Components" page

How to set Select component in Material-UI to loose its focus state after selecting one of its item

How to make component appear and set keyboard focus to its input field in iOS Safari?

Set component focus with RTTI

How to focus on component

How do I set focus on one Input instead of all in a React component?

how blur or focus vuejs component based on a vuex state

Vue. Set focus on a component's input when value in Vuex store is modified

How to set focus on a button when react component renders?

Angular set focus to an input that is in a <td> of the table Using table object in component

How to set focus on textarea which is rendered inside v-for in Vuejs 2?

Vuejs import component in another component

VueJS - Erase cookie set by the backend server via a logout component

How to set focus on child component's input on a button click in the parent in React Hooks

Vuejs - Focus element check

How to remove focus from one magicsuggest component and set focus on second magicsuggest component

How to focus certain component

Set focus on input field of the form in controlled component

Set properties/ data in VueJs Component

AS3 set focus on specific component

Vuejs VueFormGenerator focus input field

VueJS: dynamically set component's methods

To focus on a component containing an input