0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【JavaScript】画像投稿時のプレビュー機能

Posted at

画像複数投稿機能以上に詰まっていたプレビュー機能。
ようやく解決できたので備忘録のために投稿します。
jsの記述のルールや基礎的なところ抑えていかなければと思いました。

下記解決後コード
2,14,15,30行目の記述に変更を加えています。

document.addEventListener('DOMContentLoaded', function() {
  if (document.getElementById('store-image')){
    const ImageList = document.getElementById('image-list')
    
    const createImageHTML = (blob) => {
      const imageElement = document.createElement('div')
      imageElement.setAttribute('class', "image-element")
      let imageElementNum = document.querySelectorAll('.image-element').length
      
      const blobImage = document.createElement('img')
      blobImage.setAttribute('src', blob)

      const inputHTML = document.createElement('input')
      inputHTML.setAttribute('id', `store-image-${imageElementNum}`)
      inputHTML.setAttribute('name', 'store[images][]')
      inputHTML.setAttribute('type', 'file')

      imageElement.appendChild(blobImage)
      imageElement.appendChild(inputHTML)
      ImageList.appendChild(imageElement)

      inputHTML.addEventListener('change', (e) => {
        file = e.target.files[0];
        blob = window.URL.createObjectURL(file);

        createImageHTML(blob)
      })
    }

    document.getElementById('store-image').addEventListener('change', (e) => {
      let file = e.target.files[0];
      let blob = window.URL.createObjectURL(file);

      createImageHTML(blob)
    });
  };
});
  <div class="field" >
    <%= f.label :images, "店舗画像" %><br />
    <%= f.file_field :images, name: 'store[images][]' ,id:"store-image"%>
  </div>

  
以下解決前コード
2,14,15,30行目の記述に誤りがあったようです。(アンダーバーのせいでしょうか)

document.addEventListener('DOMContentLoaded', function() {
  if (document.getElementById('store_image')){
    const ImageList = document.getElementById('image-list')
    
    const createImageHTML = (blob) => {
      const imageElement = document.createElement('div')
      imageElement.setAttribute('class', "image-element")
      let imageElementNum = document.querySelectorAll('.image-element').length
      
      const blobImage = document.createElement('img')
      blobImage.setAttribute('src', blob)

      const inputHTML = document.createElement('input')
      inputHTML.setAttribute('id', `store_image${imageElementNum}`)
      inputHTML.setAttribute('name', 'store[images][]')
      inputHTML.setAttribute('type', 'file')

      imageElement.appendChild(blobImage)
      imageElement.appendChild(inputHTML)
      ImageList.appendChild(imageElement)

      inputHTML.addEventListener('change', (e) => {
        file = e.target.files[0];
        blob = window.URL.createObjectURL(file);

        createImageHTML(blob)
      })
    }

    document.getElementById('store_image').addEventListener('change', (e) => {
      let file = e.target.files[0];
      let blob = window.URL.createObjectURL(file);

      createImageHTML(blob)
    });
  };
});

  
viewの方では無理矢理画像追加のボタンを2つ用意して投稿していました。

  <div class="field" >
    <%= f.label :images, "店舗画像" %><br />
    <%= f.file_field :images, multiple: true %>
    <%= f.file_field :images, multiple: true %>
  </div>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?