S
S
SM_ST2021-11-29 14:03:14
Vue.js
SM_ST, 2021-11-29 14:03:14

How to get all inputs from ref in Vue3?

Hello!

Tell me how to get an array of inputs?

now in the onFocus function it displays only the last input, the rest he does not see

where he screwed up? Thank you

<template>
  <div>
    <div :class="$style.inputVerify">
      <input
        v-for="(v, index) in values"
        ref="inputRefs"
        :key="index"
        type="number"
        required="true"
        pattern="[0-9]"
        maxlength="1"
        :class="$style.inputVerifyItem"
        :autoFocus="index === autoFocusIndex"
        :data-id="index"
        :value="v"
        @input="onValueChange"
        @click="onFocus"
        @keydown="keyPres"
      />
    </div>
  </div>
</template>


<script lang="ts">
import { defineComponent, ref } from "vue";

export default defineComponent({
  props: {
    fields: {
      type: Number,
      default: 4,
    },
    timerSeconds: {
      type: Number,
      default: 20,
    },
  },

  setup(props, { emit }) {
    const autoFocusIndex = ref(0);
    const values = ref<any[]>([]);
    const inputRefs = ref<any>([]);
    const fields = ref(props.fields) as any;
    const seconds = ref(props.timerSeconds) as any;
    const timerInterval = ref() as any;

    for (let i = 0; i < fields.value; i++) {
      values.value.push("");
    }

    const onFocus = (e: any) => {
      const index = parseInt(e.target.getAttribute("data-id"));

      console.log(index)

      inputRefs.value[index] = "";
      values.value[index] = "";

      if (!values.value[index] && index !== 0) {
        console.log(inputRefs.value)
      }
    };

    return {
      onFocus,
    };
  },
});
</script>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SM_ST, 2021-11-29
@SM_ST

figured it out, did it
:ref="el => { if (el) inputRefs[index] = el }"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question