Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open file dialog when input type button

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" />
like image 491
Amin Uddin Avatar asked Nov 12 '14 03:11

Amin Uddin


2 Answers

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/

like image 200
p e p Avatar answered Sep 21 '22 08:09

p e p


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"/>
like image 25
Arun P Johny Avatar answered Sep 20 '22 08:09

Arun P Johny