Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

v-file-input .click() is not a function

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.

like image 574
Kobe Forrosuelo Avatar asked Jul 22 '20 08:07

Kobe Forrosuelo


People also ask

How to programmatically click the input file element in HTML?

document.getElementById ('your_input_type_file_element_id').click (); Example 1: We want to click the input file element automatically (programmatically). When the user clicks one button which is not the ‘file upload’ button of the input type file element, we can achieve it by using the following code.

How to programmatically fire click events on the input file element?

In this article, we will learn how to programmatically fire click events on the input file element. Approach: Whenever you want to perform a click event programmatically, at your specific condition, just use JavaScript in-built click () function by DOM object. For example: document.getElementById ('your_input_type_file_element_id').click ();

How to perform a click event programmatically using JavaScript?

Approach: Whenever you want to perform a click event programmatically, at your specific condition, just use JavaScript in-built click () function by DOM object. For example: document.getElementById ('your_input_type_file_element_id').click (); Example 1: We want to click the input file element automatically (programmatically).

What is the use of onclick and onfocus?

The property value onclick=”initialize ()” is used to specify the function call when the user has clicked on the input. The variable theFile is created and the input element is selected using its id. The “document.body.onfocus = checkIt” line defines that the onfocus event is fired when an element gets focused.


3 Answers

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

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

like image 62
renen Avatar answered Oct 23 '22 04:10

renen


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()
  }
}
like image 31
deirdreamuel Avatar answered Oct 23 '22 05:10

deirdreamuel


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
})
like image 1
micky canela Avatar answered Oct 23 '22 04:10

micky canela