How to Upload and Preview Images — JavaScript

A step by step guide on uploading images on the frontend

Miguel Z. Nunez
4 min readAug 8, 2021

How to upload and display images? You’ve come to the right place. In this tutorial, I’ll teach you how to do this easily with JavaScript. Before we start, I want to point out that this guide only shows you how to upload one image at a time. If you want to learn how to upload multiple images at a time, you can read this guide and if you want to learn how to upload and save images (so they’re still there when you click the refresh button), you can read this guide.

In this guide, you’ll learn how to build the following app:

How to Upload and Preview Images with JavaScript

Let’s get started.

HTML:

Create an input field of type file with an accept attribute that indicates the image file types you want to accept. I went ahead and accepted the following: JPEG, PNG, and JPG. The purpose of the output element is simply to act as a container that displays the images.

<input type="file" accept="image/jpeg, image/png, image/jpg">
<output></output>

JavaScript:

--

--