17

I was trying to programmatically trigger .click() event of v-file-input because it has on the documentation at Vuetify.

Vuetify v-file-input events docs

but it shows an error this.$refs.imagePicker.click is not a function am I missing something here?

codepen reproduction error

Code Reproduction: https://codepen.io/kforr24/pen/ZEQweLP

I'm trying to upload an image using a certain button. Like that certain button would trigger a click event on the v-file-input.

6
  • 2
    'click' is the name of the event $emitted by Vuetify, not the internal method name used to trigger that emit. If you provided more info about what you're trying to do with the programmatic trigger, you could get more help trying to solve the problem. Commented Jul 22, 2020 at 8:53
  • So, what would be the work around, same as the way as clicking an html <input> tag using a certain button? Commented Jul 22, 2020 at 9:01
  • I'm not sure if the function name is onclick. Can you try that? Commented Jul 22, 2020 at 9:04
  • @MaxPeng onClick is triggered when you click the input, not clicking/activating it. Commented Jul 22, 2020 at 9:07
  • 13
    You can do this.$refs.image.$refs.input.click(). Commented Jul 22, 2020 at 9:28

4 Answers 4

33

@User28's solution (one of the comments below the question) works:

this.$refs.image.$refs.input.click()

Sign up to request clarification or add additional context in comments.

1 Comment

Working solution for vue: "^2.6.9"
5

For vue 3, refs are deprecated. I found that a workaround this is by using element id. You can use

<v-file-input
  id="fileUpload"
  accept=".pdf"
  hide-input
  prepend-icon=""
/>
<div>
  <v-btn @click="getFile">
    UPLOAD
  </v-btn>
</div>

then on your event function for your button you can use,

const getFile = function () {
  let fileUpload = document.getElementById('fileUpload')
  if (fileUpload != null) {
    fileUpload.click()
  }
}

1 Comment

AFIK, refs are not deprecated in vue3 but it now returns a proxy instead of a reference to the DOM.
1

UPDATE February 2022

The best solution for any version is adding a id to the input or v-file-input then hide the input, in case you use v-file-input remove the icon by passing prepend-icon="" and hide-input .

<v-btn @click="document.getElementById('uploader').click()">
   Upload Button text here
   <v-file-input hide-input prepend-icon="" id="uploader"></v-file-input>
</v-btn>

in case you get this error:

TypeError: Cannot read properties of undefined (reading 'getElementById')

just put document in data

data: () => ({
    document,
    other: true
})

1 Comment

Fantastic. This worked for me, after so many failed attempts of using refs.
-2
<v-file-input ref="fileInput"/>

<v-btn @click="open">
  UPLOAD
</v-btn>

open(){
  this.$refs.fileInput.$refs.input.click()
}

1 Comment

Hello, please don't post code only and add an explantation as to why you think that this is the optimal solution. People are supposed to learn from your answer, which might not occur if they just copy paste code without knowing why it should be used.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.