Categories
JavaScript Answers

How show only button with file input?

Spread the love

Sometimes, we want to show only button with file input.

In this article, we’ll look at how to show only button with file input.

How show only button with file input?

To show only button with file input, we can hide the file input and call click on it when we click on the button.

For instance, we write

<input type="file" id="selectedFile" style="display: none" />
<input id="button" type="button" value="Browse..." />

to add the file input and the button.

We hide the file input with

style="display: none"

Then we can use it with

document.getElementById("button").onclick = () => {
  document.getElementById("selectedFile").click();
};

to select the button with

document.getElementById("button")

And then we set its onclick property to a function that clicks the file input with

document.getElementById("selectedFile").click();

to open it.

Conclusion

To show only button with file input, we can hide the file input and call click on it when we click on the button.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *