11

In html we know using input type file we get file dialog to select file. Is there any way to open file dialog using input type button? We used

<input type="file" class="file" name="attachement" />

But I want to use

<input type="button" class="file" name="attachement" />
Amin Uddin
  • 968
  • 6
  • 15
  • 31
  • answer can see here. It's working [http://stackoverflow.com/questions/210643/in-javascript-can-i-make-a-click-event-fire-programmatically-for-a-file-input](http://stackoverflow.com/questions/210643/in-javascript-can-i-make-a-click-event-fire-programmatically-for-a-file-input/36281035#36281035) – A.K. Mar 29 '16 at 10:31

8 Answers8

38

Yes - you can hide the input type="file" but still have it in your markup. You then show a regular button on your page and onclick of that button, you programmatically trigger the click event of your actual file input:

<input id="fileInput" type="file" style="display:none;" />
<input type="button" value="Choose Files!" onclick="document.getElementById('fileInput').click();" />

Fiddle: http://jsfiddle.net/cnjf50vd/

p e p
  • 6,593
  • 2
  • 23
  • 32
16

You can use a button and a hidden file element

function openAttachment() {
  document.getElementById('attachment').click();
}

function fileSelected(input){
  document.getElementById('btnAttachment').value = "File: " + input.files[0].name
}
<input type="file" class="file" id="attachment" style="display: none;" onchange="fileSelected(this)"/>
<input type="button" class="file" id="btnAttachment" onclick="openAttachment()" value="File"/>
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
4

To open a file dialog, you need a file input.

Here you can open it when clicking a button without writing any JavaScript code.

<button type="button">
  <label for="file">
    Open File Dialog
  </label>
</input>
<input type="file" id="file" style="display:none;"/>

The point of this solution is to use the label element and its for attribute. You should make the label to cover the full button to give a perfect solution.

Ever Dev
  • 1,882
  • 2
  • 14
  • 34
3

U couldn't really open file dialog when the type is a button.. U can only use by default type="button" and on mouse over the button, the type will change to type='file' like that :

<input type="button" class="file" name="attachement" onmouseover="(this.type='file')" onmouseout="(this.type='button')" value="Choose file" />
ayman lys
  • 159
  • 4
0

no. the type attribute of input specifies what type of element you are dealing with (IE: checkbox, radio, button, submit, etc), so you have to specifically state file if you want the file dialog to open. if you want a button, then you specify the input type as a button.

that's not to say you can do workarounds, but setting input type to button is not possible to force a file dialog off that button alone.

Rich
  • 181
  • 1
  • 5
0

Hence I want to avoid type file in html of a input so I forced to change such type in runtime

<input id="fileInput" type="button" style="display:none;" />
<input type="button" value="Choose Files!" onfocus="document.getElementById('fileInput').type='file'" onclick="document.getElementById('fileInput').click();" />
Amin Uddin
  • 968
  • 6
  • 15
  • 31
0

In React this is how is done

 const inputer = useRef()
 function ClickComponent(){
   return <div>
       <input ref={inputer} />
       <button onClick={inputer.current.click()}>Click me </button>
   </div>
 }
 

     
  • OP does not seem to use React. This code is equivalent to answers submitted by others in vanilla JS in that it triggers a click event on an `input[type=file]` element. – undefined Nov 25 '22 at 08:54
0

@Ever answer is better because it doesn't use javascript, but it has some issues as @Shanika said. To function we have to click on the button text, not the button. So I guess we can wrap it all with a label element. But it doesn't work yet. Next, we can wrap the button element with a div element and add pointer-events:none.

<label for="fileUpload">
  <input type="file" id="fileUpload" style="display:none;" />
  <div>
    <input type="button" value="Upload" style="pointer-events:none;" />
  </div>
</label>
Sanka Sanjeewa
  • 1,993
  • 14
  • 16