Search code examples
javascripthtmlimageinnerhtmlpathname

Javascript that automatically fills in HTML file path for images


I'm trying to use window.location.pathname and injecting innerHTML to generate the file paths for an image so all I need to do is type fileName.png in a div in the html body and have the javascript generate the file path behind it so that it displays the image in the rendered website. This is for images that aren't stored in the same folder as the working file. I've had mild success but it only works for one image per page which isn't very helpful.

I've gotten this code to work for one image per page:

<div class="picName">pic.png</div><div id=<"shortcut"></div>`

<script>
  var relativePath = window.location.pathname;
  var picName = document.getElementById('matts-shortcut').previousElementSibling.innerHTML;
  document.getElementById("matts-shortcut").innerHTML =
    '<src=\'/images' + relativePath + '/' + picName + '\'>';

</script>

Solution

  • The solution below pulls images names from with Divs using .querySelectorAll() which returns a DOM NodeList. The NodeList is useful because it has a forEach() method that can be used to loop over each item is the list. Loop over each list item using it's textContent property as the image name. Then you'll need to create a new image element for each image. To do that you can do something similar to this.

    let relativePath = "https://dummyimage.com"; // replace the url with path name (maybe window.location.path)
    // create a reference to the input list
    // querySelectorAll return a NodeList
    let inputNameList = document.querySelectorAll('.image-name');
    // Loop through each image name and append it to the DOM
    // the inputNameList (NodeList) has a "forEach" method for doing this
    inputNameList.forEach((image) => {
      let picName = image.textContent;
      // Create a new image element
      let imgEl = document.createElement('img');
      // Set the src attribute of the image element to the constructed URL
      // the name of the picture will be the div text content
      // This is done with a template literal that you can learn about here:
      // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
      imgEl.src = `${relativePath}/${image.textContent}`;
      // Now we have a real image element, but we need to place it into the DOM so it shows up
      // Clear the image name
      image.textContent = "";
      // Place the image in the Div
      image.appendChild(imgEl);
    });
    <div class="image-name">300.png</div>
    <div class="image-name">200.png</div>
    <div class="image-name">100.png</div>
    <div class="image-name">400.png</div>

    EDIT: In response to Ismael's criticism, I've edited the code slightly and commented every line so you can learn from this answer. There are two hyperlinks referenced in the code to help you think about coding in a modern way and so you can interpret modern code you read more easily.

    About:

    Edit 2: With further clarification, the answer has been amended to pull the image file names from Div elements already in the DOM.