12

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" />
1

9 Answers 9

41

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/

Sign up to request clarification or add additional context in comments.

1 Comment

@p e p, But I want to avoid input type file in my page.
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"/>

Comments

5

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>
</button>
<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.

2 Comments

This should be the correct solution! Yet there are some issues with the code
This solution doesn't allow you to open the file modal via keyboard.
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" />

Comments

1

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>
 }
 

     

1 Comment

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.
1

@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>

EDIT:

@AgainPsychoX, We can use a custom style for the button if it is possible

button {
    all: unset;

    label {
        padding: 2px 6px;
        border: 1px solid gray;
        border-radius: 3px;
        background-color: #f0f0f0;
        font-family: Arial;
        font-size: 13.5px;
    }

    &:hover,
    &:focus-within {
        label {
            border: 1px solid #4b4b4b;
            background-color: #e5e5e5;
        }
    }
}
<button>
    <label for="fileUpload2">Upload</label>
</button>
<input type="file" id="fileUpload2" style="display: none" />

@Animal Rights, I think we can't go ahead with this approach if we want to use keyboard.

2 Comments

It also isn't perfect, as button doesn't get :active / :hover events now
This solution doesn't allow you to open the file modal via keyboard.
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.

Comments

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();" />

Comments

0

You can also do it like this ...

<label><!--style the label to look like a button-->
        Choose some file
        <input type="file" style="display:none;"><!--style this to be hidden-->
</label>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.