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

211
Views
How to get input value if it is not in html

Inside the code, I create an input element with the file type. I don't add it to the tree. I want to get the value of this input after the user adds it to the file. There is one, but I do not need to add this input to the HTML template.

                    <p-splitButton
                        label="Add document"
                        icon="pi pi-plus"
                        (onClick)="openAddDocument()"
                        [model]="addDocumentButtonMenuItems"
                        styleClass="p-button-help"
                    ></p-splitButton>

public readonly addDocumentButtonMenuItems = [
    {
        label: 'Dowload document', icon: 'pi pi-upload', command: () => {
            this.uploadFile();
        },
    },
];

public uploadFile(): void {
        const input = document.createElement('input');
        input.type = 'file';
        input.click();
    }

How to get value input?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

to get file value of an input file you can use myinput.files[0];after a change event on input

but be careful in modern browser you can't open file dialog from script without a user action (click for sample)

js:14 File chooser dialog can only be shown with a user activation.

in the following link you will found a solution to open a file dialog with native js How to open a file / browse dialog using javascript?

you can bind onChange event of input file to get the chosen file

var fileInput = document.getElementById('theFile');
       fileInput.addEventListener('change', function() {
        var selectedFile = document.getElementById('theFile').files[0];
        console.log(selectedFile.name);
       }); 

function performClick(elemId) {
       var elem = document.getElementById(elemId);
       if(elem && document.createEvent) {
          var evt = document.createEvent("MouseEvents");
          evt.initEvent("click", true, false);
          elem.dispatchEvent(evt);
       }
    }
    
   var fileInput = document.getElementById('theFile');
   fileInput.addEventListener('change', function() {
    var selectedFile = document.getElementById('theFile').files[0];
    console.log(selectedFile.name);
   }); 
    <a href="#" onclick="performClick('theFile');">Open file dialog</a>
    <input type="file" id="theFile" />

about 4 years ago · Santiago Trujillo 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!