Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                 <div class="container">
        <h1>Vanilla js input file multiple with preview</h1>
        <div class="file-multiple">
            <input type="file" multiple  accept="image/*">
            <label>Input 1</label>
            <div class="file-multiple__btn">
                Select file <span>upload</button>
            </div>
            <div class="file-multiple__previews"></div>
        </div>
        <div class="file-multiple">
            <input type="file" multiple  accept="image/*">
            <label>Input 2</label>
            <div class="file-multiple__btn">
                Select file <span>upload</button>
            </div>
            <div class="file-multiple__previews"></div>
        </div>
    </div>


    <div class="feedback">
        Any bug? Contact with me <a href="mailto:alex.titakoff@yandex.ru">alex.titakoff@yandex.ru</a>
    </div>

              
            
!

CSS

              
                 body {
            background: rgb(255, 255, 255);
            background: linear-gradient(187deg, rgba(255, 255, 255, 1) 0%, rgba(106, 90, 205, 0.2872740160517332) 100%);
            min-height: 100vh;
            font-family: sans-serif;
        }

        h1 {
            text-align: center;
            padding: 0 12%;
            margin-top: 15vh;
        }

        .container {
            max-width: 620px;
            display: flex;
            /* justify-content: center; */
            margin: auto;
            flex-wrap: wrap;

        }

        .feedback {
            position: absolute;
            bottom: 20px;
            text-align: center;
            width: 100%;
        }

        a {
            color: slateblue;
            font-weight: bold;
        }

        /**
        File input styles
        */
        .file-multiple {
            margin-bottom: 20px;

            font-size: 14px;

            padding: 0 12%;
        }

        .file-multiple label {
            font-weight: bold;
            margin-bottom: 5px;
            display: inline-block;
        }

        .file-multiple input[type="file"] {
            display: none;
        }

        .file-multiple__btn {
            border: 1px solid slateblue;
            display: inline-block;

            border-radius: 4px;
            overflow: hidden;
            padding-left: 8px;
            width: 200px;
            display: flex;
            justify-content: space-between;
            flex-wrap: wrap;
            align-items: center;
        }

        .file-multiple__btn span {
            background: slateblue;
            border: none;
            outline: none;
            color: #fff;
            padding: 8px 10px;
            cursor: pointer;

        }

        .file-multiple__previews {
            display: flex;
            margin-top: 10px;
            flex-wrap: wrap;
        }

        .file-multiple__preview {
            width: 150px;
            height: 120px;
            position: relative;
            -webkit-box-shadow: 4px 4px 8px 0px rgba(34, 60, 80, 0.2);
            -moz-box-shadow: 4px 4px 8px 0px rgba(34, 60, 80, 0.2);
            box-shadow: 4px 4px 8px 0px rgba(34, 60, 80, 0.2);
            margin-right: 5px;
            margin-bottom: 5px;
        }

        .file-multiple__preview img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }

        .file-multiple__remove-icon {
            cursor: pointer;
            width: 30px;
            height: 30px;
            position: absolute;
            top: 0;
            background: #fff;
            border-radius: 50%;
            right: 0;
            -webkit-box-shadow: 4px 4px 8px 0px rgba(34, 60, 80, 0.2);
            -moz-box-shadow: 4px 4px 8px 0px rgba(34, 60, 80, 0.2);
            box-shadow: 4px 4px 8px 0px rgba(34, 60, 80, 0.2);
        }

        .file-multiple__remove-icon svg {
            width: 77%;
            height: auto;
            position: relative;
            left: 11%;
            top: 13%;
        }
              
            
!

JS

              
                  let multipleInputs = document.querySelectorAll('.file-multiple')
        // init multiple blocks
        multipleInputs.forEach(multipleInput => {
            const input = multipleInput.querySelector('input')
            

            // files storage
            let inputFilesList = Array();

            input.addEventListener('change', function (e) {

                // check and append new files
                appendNewFiles(input,inputFilesList)

                // render preview
                showMultiplePreviewImg(input.files, multipleInput)
                
                // remove handler
                const removePreviewBtns = multipleInput.querySelectorAll('.file-multiple__remove-icon')
                removePreviews(removePreviewBtns, input)

                // console.log(input.files);

            })
            const uploadBtn = multipleInput.querySelector('span')
            uploadBtn.addEventListener('click', function (e) {
                input.click()
                // save to storage
                inputFilesList = []

                Array.from(input.files).forEach(file => {
                    inputFilesList.push(file);
                })
            })
        })

        function appendNewFiles(input,inputFilesList) {
            // append new file and check if exist in filelist
            let dt = new DataTransfer();
            Array.from(input.files).forEach(file => {
                // console.log(inputFilesList);

                let noExist = true
                if (inputFilesList.length) {
                    // check exist

                   
                    inputFilesList.forEach(item => {
                        if ((item.lastModified + item.name + item.size)  == (file.lastModified + file.name + file.size)) {
                            noExist = false
                        }
                    })

                    // append new file if no exist
                    if (noExist) {
                        dt.items.add(file);
                    }
                } else {
                    dt.items.add(file);
                }
            })
            // copy files from storage
            inputFilesList.forEach(item => {
                dt.items.add(item);
            })

            input.files = dt.files
        }

        // remove preview and file object
        function removePreviews(btns, input) {
            btns.forEach(btn => {
                btn.addEventListener('click', function () {
                    btn.parentNode.remove()
                    let hash = btn.getAttribute('data-hash')

                    let dt = new DataTransfer();
                    for (let i = 0; i < input.files.length; i++) {
                        const file = input.files[i]
                        if (hash != (file.lastModified + file.name + file.size)) {
                            // console.log(file);
                            dt.items.add(file);
                        }
                    }
                    input.files = dt.files
                    // console.log(input.files);
                })
            })

        }

        // функция рендер превью
        function showMultiplePreviewImg(files, wrapper) {
            //clear 
            wrapper.querySelector('.file-multiple__previews').innerHTML = ""

            const fileListArr = Array.from(files)
            fileListArr.forEach(file => {
                let imgSrc = URL.createObjectURL(file)
                let imgBlock = `<div class="file-multiple__preview"> <img src=${imgSrc}>  <div class="file-multiple__remove-icon" data-hash = "${file.lastModified}${file.name}${file.size}"><svg xmlns="http://www.w3.org/2000/svg" width="72px" height="72px" viewBox="0 0 72 72">
                      <g id="line">
                        <line x1="17.5" x2="54.5" y1="17.5" y2="54.5" fill="none" stroke="#000000" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" stroke-width="2"/>
                        <line x1="54.5" x2="17.5" y1="17.5" y2="54.5" fill="none" stroke="#000000" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" stroke-width="2"/>
                      </g>
                    </svg></div></div>`;

                wrapper.querySelector('.file-multiple__previews').insertAdjacentHTML('beforeend', imgBlock)
            });
        }
              
            
!
999px

Console