Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

177
Views
How do get the value of input element of type "file"

I have tried to solve this problem without success. I want to get the "value" of an input element of "type = file". I don't know what I am doing wrong that's preventing me from getting the "input value".

function addFile() {

  let toUpload = document.getElementById("fileid").click();
  console.log(toUpload.value);
  //Do something with file name;
}
<button class=" btn border border-1
  rounded-pill " id="add-btn" onclick="addFile()">
    <i class="bx bx-plus-medical">
    </i>
    Choose file                  
</button>

<input id='fileid' type='file' hidden/>

The file upload dialogue comes up. But I cannot access the file name. Who can figure out an issue with the above code please?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You have three different problems here.

  1. click() doesn't return the element, and the element has the value. Store the element in a variable and use that for reading and for clicking.
  2. You need to wait until the user picks a file before reading the value, currently you are trying to get the value before it has changed. Use an event listener.
  3. The value is an obfuscated path. Use the files property to get details of the files selected.

function addFile() {
  let toUpload = document.getElementById("fileid");
  toUpload.addEventListener('change', event => {
      //Do something with file name;
      console.log(toUpload.value);  
      console.log(toUpload.files[0].name);
  }, { once: true });
  toUpload.click();
}
<button class=" btn border border-1
  rounded-pill " id="add-btn" onclick="addFile()">
    <i class="bx bx-plus-medical">
    </i>
    Choose file                  
</button>

<input id='fileid' type='file' hidden/>

about 4 years ago · Juan Pablo Isaza Report

0

input type file doesn't have a value, it will have a property called files

files is an array of files (in case the input allows multiple), each file will have a property called name to get the display name

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!