sutty/app/javascript/etc/image_preview.js

12 lines
368 B
JavaScript
Raw Normal View History

2020-11-20 00:23:23 +00:00
document.addEventListener('turbolinks:load', () => {
document.querySelectorAll('input[type=file]').forEach(file => {
if (!file.dataset.preview) return
2019-08-22 01:09:29 +00:00
2020-11-20 00:23:23 +00:00
file.addEventListener('change', event => {
if (file.files.length === 0) return
2019-08-22 01:09:29 +00:00
2020-11-20 00:23:23 +00:00
document.querySelector('#' + file.dataset.preview).src = window.URL.createObjectURL(file.files[0])
})
})
})