0

I would like to make a file input button like the one in Amazon write a review pictured below

amazon upload button

How is it possible that the entire button opens up the file dialog on click if the input file type is hidden?

amazon upload photo button code

My code here hides the input file type button but does not open the file dialog. It is using React with tailwind classes

    <button
      type="button"
      className="p-6"
    >
      <div aria-label="Add a photo">
        <HiOutlinePlus className="text-4xl" />
        <input type="file" accept="image/*" className="hidden" />
      </div>
    </button>
1
  • You can use ref to get reference of the input, and then when the add button is clicked, click the input with code Commented Apr 20, 2022 at 4:12

1 Answer 1

3

How is it possible that the entire button opens up the file dialog on click if the input file type is hidden?

The probably use a separate widget that then opens the hidden file input on click, as follows:

function openFileUpload() {
  document.getElementById("hiddenFile").click();
}
<button onclick="openFileUpload()" type="button">Visible Button</button>
<input type="file" id="hiddenFile" style="visibility:hidden">

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

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.