document.addEventListener('turbolinks:load', () => { document.querySelectorAll('input[type=file]').forEach(file => { if (!file.dataset.preview) return file.addEventListener('change', event => { if (file.files.length === 0) return document.querySelector('#' + file.dataset.preview).src = window.URL.createObjectURL(file.files[0]) }) }) })