Add some spice to your HTML file fields with an image preview pane
Tags: javascript, html, css, webdev
Published: 2021-11-10 19:20:16

The default HTML file field is rather boring. It's quite painful to style and not really flexible either.   

![HTML File field](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8vfq0siar8uusygjhkx0.png)

With a little bit of JavaScript though, we can spice up the file input field a bit and add a preview pane for images; so the user can see the image they've selected before submitting the form.  

![HTML File field demo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6wpku0rtw16jqrrb8sol.png)

A good way to encapsulate the logic for this field is to use a [JavaScript Custom Element](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements). We'll create a class called `ImageInputField` and define it to use the tag name `image-input-field`. Let's start with our HTML markup:  
  

```html
<image-input-field>
  <img preview>
  <input type="file" name="logo">
  <button type="button" select>Select Image</button>
  <button type="button" remove>Remove Image</button>
</image-input-field>
```

The above code should be pretty self explanatory. To bring this to life, we need to create and define our custom element.

```javascript
export class ImageInputField extends HTMLElement {
  connectedCallback() { 
    // Configure click listeners for the two buttons
    // and a change listener for the input field
    this.configureListeners()
    
    // Hide the remove button by default as initially
    // there won't be a file selected
    this.removeButton.style.display = "none"
 
    // Hide the input field as it's only used under
    // the hood.
    // The user clicks on the "Select Image" button
    this.input.style.display = "none"
 
    // Restrict the input field to images only
    this.input.accept="image/*"
  }

  get input() {
    return this.querySelector("input[type=file]")
  }

  get selectButton() {
    return this.querySelector("button[select]")
  }

  get removeButton() {
    return this.querySelector("button[remove]")
  }

  get preview() {
    return this.querySelector("img[preview]")
  }

  removeImage() {
    this.preview.removeAttribute("src")
    this.input.value = ""
    this.removeButton.style.display = "none"
  }
 
  // Read the image off the disk and set it to our img element
  showPreview(image) {
    let reader = new FileReader();
    reader.onload = (event) => {
      this.preview.setAttribute("src", event.target.result)
    }

    reader.readAsDataURL(image);
    this.removeButton.style.removeProperty("display")
  }

  configureListeners() {
    this.input.addEventListener('change', event => {
      let file = event.target.files[0]
      this.showPreview(file)
    })

    this.selectButton.addEventListener('click', () => {
      this.input.click()
    })

    this.removeButton.addEventListener('click', () => {
      this.removeImage()
    })
  }
} 
 
// Register our custom element with the CustomElementRegistry
customElements.define('image-input-field', ImageInputField)
```

With the above element, our component is complete. Users will now see a preview of the image they've selected. We're also free to style any of the contained elements as we wish. So for example, we might want to limit the width of the image preview so a large image doesn't mess up the layout of the whole page:

```css
image-input-field img {
  display: block;
  max-width: 200px;
}
```
   
[Here's a CodePen](https://codepen.io/ayushn21/pen/KKWzEQj) demonstrating the component in action!&nbsp;