title: Export part of JS function into main.js
body: <p>Hello Stackoverflow community!</p>
  <p>I'm currently in the process of learning by doing by writing a website and I'm unfortunately not sure how to address certain concepts but I will do my best.</p>
  <p>I'm using Bootstrap 4 and wrote a website with several sub-pages with an external(?) js-file.
  Now to my actual question:
  I have written this js code (A) to show the next image in an array when I click on the &quot;main image&quot; (id:expandedImg) but I can't figure out how to export this code, or what the best practice is.
  It would be nice to have the Array embedded in the HTML file and the rest in main.js.
  But since I refer to the variable imagesArr I'm not sure how to do that.</p>
  <p>(A)</p>
  <pre><code>//Array of img
  
  const imagesArr = [{
              image: '###',
              alt: 'nr1',
          },
          {
              image: '###',
              alt: 'nr2',
          },
          {
              image: '###',
              alt: 'nr3'
          },
      ];
  
      const createImage = (image, alt, index) =&gt; {
          return `&lt;img src=&quot;${image}&quot; alt=&quot;${alt}&quot; class=&quot;img-thumbnail border-0 img-thumbnail-desktop&quot; onClick=&quot;expandingImage(this)&quot; currentimage=&quot;${index}&quot;/&gt;`;
      };
  
  
      // Logic, this should be exported to main.js
  
  
      const createImages = (images) =&gt; {
          let final = '';
          for (let i = 0; i &lt; images.length; i++) {
              const e = images[i];
              final += createImage(e.image, e.alt, i);
          }
          return final;
      }
  
      document.addEventListener(&quot;DOMContentLoaded&quot;, function () {
          console.log('Loaded')
  
          const container = document.querySelector('.thumbnailContainer');
  
          container.innerHTML = createImages(imagesArr)
      });
  
      const nextImage = (img) =&gt; {
          const current = +img.getAttribute('currentimage');
          if (current &lt; imagesArr.length - 1) {
              img.setAttribute('src', imagesArr[current + 1].image)
              img.setAttribute('currentimage', current + 1)
          }
      }
  
      function expandingImage(imgs) {
          const expandImg = document.getElementById(&quot;expandedImg&quot;);
          const imgText = document.getElementById(&quot;imgtext&quot;);
          expandImg.src = imgs.src;
          imgText.innerHTML = imgs.alt;
          expandImg.setAttribute(&quot;currentimage&quot;, imgs.getAttribute('currentimage'))
          expandImg.parentElement.style.display = &quot;block&quot;;
      }
  </code></pre>
  <p>I could embed this just in every subpage and it will work, but I'm pretty sure that this would be bad code...</p>
  <p>My second question is if I can have a js-function to fill or refer to certain img-src.
  For example when I have this HTML code (B) if I can somehow copy-paste or refer to the const I declared as js-code, so I don't have to write basically the same thing twice.</p>
  <p>(B)</p>
  <pre><code>&lt;div class=&quot;thumbnailContainer&quot;&gt;
  &lt;img src=&quot;#####&quot; alt=&quot;nr1&quot; class=&quot;img-thumbnail border-0 img-thumbnail-desktop&quot; onclick=&quot;expandingImage(this);&quot;&gt;
  &lt;img src=&quot;#####&quot; alt=&quot;nr2&quot; class=&quot;img-thumbnail border-0 img-thumbnail-desktop&quot; onclick=&quot;expandingImage(this);&quot;&gt;
  &lt;img src=&quot;#####&quot; alt=&quot;nr3&quot; class=&quot;img-thumbnail border-0 img-thumbnail-desktop&quot; onclick=&quot;expandingImage(this);&quot;&gt;
  </code></pre>
    
  <p>This problem is surely trivial but I'd be really thankful if you guys can help me out.
  I'll try my best on my own but it would be glad to know what the best practice is, in this kind of situation.</p>
  <p>Have a good day/evening!</p>
  <p>K</p>