Automatically showcase images from a directory upon webpage loading

Is there a way to modify my code so that the images from the first directory are displayed on the page when it loads, instead of waiting for a menu option to be clicked? The page looks empty until a menu option is selected, and I would like the images to show up beforehand. Here is the current code, any suggestions are appreciated. Thank you.

< script >
  $(document).ready(function() {
    $("a").click(function() {
      var dir_path = $(this).data("albumid");
      
      LoadGallery(dir_path);
      return false;
    });
  });

function LoadGallery(dir_path) {
  $.ajax({
    url: dir_path,
    success: function(data) {
      $(".image-container").empty();

      $(data).find("a:contains(.jpg), a:contains(.png), a:contains(.jpeg)").each(function() {
        this.href.replace(window.location.host, "").replace("http:///", "");
        var file = dir_path + $(this).text();
        $(".image-container").append($("<a href='javascript:;' class='thumb' data-src='" + file + "'><img src='" + file + "' title='Click to enlarge' alt='#'/></a>"));

        if ($(".image-container").children("a").length === 30) {
          return false;
        }
      });

      $(".image-container").append("<strong><p>Click on a thumb nail to show a larger image.</p></strong>");

      $(".thumb").bind('click', function() {
        var Popup = "<div class='bg'></div>" + "<div class='wrapper'><img src='<img src=''/>" + "<label href='javascript:;' class='prev-image'>«</label><label href='javascript:;' class='next-image'>»</label><a href='javascript:;' class='close' title='Close'>Close</a>";
        var Img = $(this).attr("data-src");
        $("body").prepend(Popup);
        $(".bg").height($(window).height() * 4);
        $(".wrapper img").attr("src", Img);

        $(".prev-image").bind('click', function() {
          var prev = $(".image-container").find("img[src='" + Img + "']").parent().prev('a').find("img").attr('src');
          if (!prev || prev.length === 0)
            return false;
          else {
            $(".wrapper img").attr("src", prev);
            Img = prev;
          }
        });

        $(".next-image").bind('click', function() {
          var next = $(".image-container").find("img[src='" + Img + "']").parent().next('a').find("img").attr('src');
          if (!next || next.length === 0)
            return false;
          else {
            $(".wrapper img").attr("src", next);
            Img = next;
          }
        });

        $(".close").bind('click', function() {
          $(this).siblings("img").attr("src", "")
            .closest(".wrapper").remove();
          $(".bg").remove();
        });
      });
    }
  });
}; < /script>
@import url(http://fonts.googleapis.com/css?family=Varela+Round);
 #nav {
  float: left;
  width: 20px;
  margin: 10px 10px 20px 0px;
}
#nav,
#nav ul {
  list-style: none;
  padding: 0;
}
#nav a {
  position: relative;
  display: block;
  width: 105px;
  padding-left: 10px;
  margin: 3px 0;
  text-decoration: none;
  font-family: Geneva, Arial, Helvetica, sans-serif;
  font-variant: small-caps;
  font-weight: bold;
  color: #fff;
}
#nav a:link,
#nav a:visited {
  border-left: #00425E solid 10px;
  color: #fff;
}
#nav a:hover,
#nav a:active {
  border-left-color: #fff;
  background-color: #770709;
  color: #fff;
}
#nav a#here {
  border-left-color: #fff;
  background-color: transparent;
  color: #fff;
}
#nav ul {
  margin-left: 20px;
}
#nav ul a {
  width: 50px;
  color: #fff;
}
.image-container {
  padding-top: 50px;
}
.image-container img {
  background-color: white;
  border: 4px solid #444;
  box-shadow: 0 0 5px #222;
  padding: 3px;
  margin-top: 10px;
  height: auto;
  width: auto;
  max-width: 100px;
  max-height: 100px;
  transition: all .7s ease-in-out;
}
.image-container img:hover {
  border: 4px solid #888;
  cursor: zoom-in;
}
.bg {
  background-color: #333;
  filter: alpha(opacity=70);
  left: 0;
  opacity: 0.7;
  position: fixed;
  top: 0;
  width: 100%;
  z-index: 1000;
}
.wrapper {
  background-color: white;
  border: 3px solid #444;
  box-shadow: 0 0 5px #222;
  padding: 3px;
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 1001;
}
.wrapper .next-image {
  position: absolute;
  font-size: 2.8em;
  top: 50%;
  color: #999;
  width: 45px;
  line-height: 30px;
  text-align: center;
  height: 45px;
  border-radius: 100%;
  opacity: 0.7;
  filter: alpha(opacity=40);
  /* For IE8 and earlier */
  right: 10px;
  left: auto;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}
.wrapper .prev-image {
  position: absolute;
  font-size: 2.8em;
  top: 50%;
  color: #999;
  width: 45px;
  line-height: 30px;
  text-align: center;
  height: 45px;
  border-radius: 100%;
  opacity: 0.7;
  filter: alpha(opacity=40);
  /* For IE8 and earlier */
  left: 10px;
  right: auto;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}
.wrapper .prev-image:hover {
  border: 2px solid #ccc;
  cursor: pointer;
  opacity: 1;
}
.wrapper .next-image:hover {
  border: 2px solid #ccc;
  cursor: pointer;
  opacity: 1;
}
.close {
  background: transparent url(../gallery/close.png) no-repeat;
  height: 32px;
  position: absolute;
  right: -16px;
  text-indent: -9999px;
  top: -16px;
  width: 32px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="content-container" style="height: 600px;">
  <div id="content" style="height: 600px;">
    <div style="float: left; width: 200px;">
      <h1> <span>The Gallery</span> </h1>
      <ul id="nav">
        <li><a href="" id="here">Gallery</a>
          <ul>
            <li><a href="#" data-albumid="images/gallery/fld01/">2014</a>
            </li>
            <li><a href="#" data-albumid="images/gallery/fld02/">2014</a>
            </li>
            <li><a href="#" data-albumid="images/gallery/fld03/">2014</a>
            </li>
            <li><a href="#" data-albumid="images/gallery/fld04/">2015</a>
            </li>
            <li><a href="#" data-albumid="images/gallery/fld05/">2015</a>
            </li>
          </ul>
        </li>
        <li><a href="../index.html">Back to home</a>
        </li>
      </ul>
    </div>
    <div class="image-container">
      <strong><p>Select a menu option to display a list of thumb nails.</p></strong>
    </div>

    <div class="clear"></div>
  </div>
</div>

Answer №1

To implement this functionality, you need to insert the code

LoadGallery($('a[data-albumid]:first').data('albumid'));
inside your $(document).ready() function as shown below. This code will target the first a element with the data-albumid attribute and load the gallery accordingly.

$(document).ready(function () {
    LoadGallery($('a[data-albumid]:first').data('albumid'));

    $("a").click(function () {
        var dir_path = $(this).data("albumid");
        //var txt=$(this).text();
        //$(this).text(txt);
        LoadGallery(dir_path);
        return false;
    });
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Populate a database with information collected from a dynamic form submission

I recently created a dynamic form where users can add multiple fields as needed. However, I'm facing a challenge when it comes to saving this data into the database. You can view a similar code snippet for my form here. <form id="addFields" me ...

Once the form has been submitted, proceed to open a new page following processing

Trying to remove an entry or offer from the database and then return to the page with all entries/offers. Attempted using Javascript, but it successfully deletes the item without redirecting to the overview page. Is this the correct approach with Javascri ...

The Node.js server is outputting an HTTP status code of 404

I have recently set up a small server. Interestingly, when I attempt to perform a GET request to my server through a browser, I can see the correct data. However, when I try to make a POST request to my server using code, I receive an HTTP status 404 error ...

Improving Page Load Speed with HTML Caching: Strategies for Enhancing Performance when over half of the data transferred is for navigation menus

I manage a complex and expansive website that contains a significant amount of repetitive HTML elements such as the navigation menu and top ribbon. Loading a single page on my site can be resource-intensive, with up to 300KB of data required, half of whic ...

I'm baffled by why I keep receiving the error message "Unknown provider: $routeProvider <- $route <- AppController" in AngularJS, even though I have already

I've exhausted all the solutions I found on stackoverflow without success. Apologies if this is a duplicate question. My goal is to reset the content of my Bootstrap table with a button click using the function $route.reload(). However, when I includ ...

What is the process for modifying a field type in Netsuite?

I'm exploring ways to build an online form within Netsuite. Our predefined fields include firstname, lastname, etc. Within these, we also have a NLSUBSCRIPTIONS tag, but it's currently set up as a drop-down with multiple select options. I ...

Creating a multi-column layout with HTML and CSS

I'm currently exploring the most effective method to design this concept: https://i.stack.imgur.com/SOQp4.png The black square symbolizes small icons, accompanied by a heading and paragraph for each specific section. The icons need to align with the ...

Difficulty encountered when positioning a container using BootStrap 4

As a newcomer to the world of web development, I encountered an issue while trying to align a container on my webpage. Despite my efforts to center the container using (line 30), the page seems to update but there is no visible change. Can anyone help me ...

Continuously send AJAX requests using jQuery until the files have been found

Does anyone know how to continuously listen for an AJAX response from a server-side file using jQuery? The issue arises when the file is only generated after a post request is made from another computer or browser. I need a solution that prevents a 404 e ...

Replace Original Image with Alternate Image if Image Error Occurs Using jQuery (Bootstrap File Input)

I am currently utilizing the Bootstrap File Input component for file uploads, and it's working splendidly. I have set up the initialPreviewConfig to display the existing image on the server. However, there are instances when there is no file available ...

Establish a table containing rows derived from an object

I am currently dealing with a challenge in creating a table that contains an array of nested objects. The array I have follows this schema: array = [ { id: 'Column1', rows: { row1: 'A', row2 ...

Is it possible to reset the text within the text box when the form is being submitted using the load() ajax function?

I am working on implementing a comment feature where the data entered in the form is appended and displayed after submission. Here is my HTML form: <table> <tr><td>Name :</td><td> <input type="text" id="name"/></td&g ...

The Jquery .submit() method seems to be malfunctioning following the execution of the .ajax() function

JQuery .submit() function is experiencing issues after the .ajax() function has run. CODE: if ($("#NetBanxFormTag").attr("action") != "") { AddOdemeLog($("#<%=TxtCC.ClientID %>").val(),MusteriAdSoyad,$("#<%=HFDrm.ClientID %>").val(), $ ...

Implementing automatic token refreshing and automatic logout features in Vue

I am a novice web developer looking to enhance my skills. For my initial project, I decided to incorporate Laravel and Vue. My main objectives are to: Implement an auto-logout feature after 3 minutes of user inactivity Create an automatic ping to my token ...

Changing a string into a JavaScript date object

I am encountering an issue where I have a string retrieved from a JSON object and attempting to convert it to a JavaScript date variable. However, every time I try this, it returns an invalid date. Any insights into why this might be happening? jsonObj["d ...

Retrieve the Data from Input Fields with Matching Classes and Transmit to a Script Using AJAX Request

I am working on a form that includes multiple input fields: <input type="text" class="date" name="date[]" onkeyup="showHint()" /> <input type="text" class="date" name="date[]" onkeyup="showHint()" /> <input type="text" class="date" name="da ...

Combine strings in an array of objects

I have an array containing objects with a "Closed" property that holds numerical values. I want to loop through the array and concatenate all the "Closed" property values found in each object. For example, in the given array, the final result should be 12 ...

Stop the bootstrap carousel at a specified screen size

Looking for a way to stop the bootstrap carousel from auto-playing on screen sizes below 640px. Despite trying various methods, I have yet to find a solution that works. This is my current approach: $(document).ready(function(){ if ($(window). ...

I would like to apply my CSS styles to all the hyperlinks on my website

This is the CSS code I created: img{width:85%; height:auto;} #thumbwrap { position:relative; } .thumb img { border:1px solid #000; margin:3px; float:left; } .thumb span { position:absolute; visibility:hidden; } .thumb:hover, .thu ...

What is the method for retrieving the name of the currently selected HTML element?

After using jQuery to select certain tags, I am now trying to obtain the name of each tag: $('select, :checkbox, :radio').each(function(){ // ... }); To accomplish this, I have attempted the following code: $('select, :checkbox, :radio ...