What could be causing the alt tag to not appear in the overlay when an image is clicked?

I am struggling with an issue where the alt text for the image should be added dynamically using JavaScript and displayed beneath the image in an Overlay. However, when I click on an image, it appears in the Overlay but the alt text is not showing up. Any suggestions or help would be greatly appreciated!

HTML:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Zack Adams | Web Developer</title>
    <link rel="stylesheet" href="css/normalize.css">
    <link href='https://fonts.googleapis.com/css?family=Lobster' rel='stylesheet'             type='text/css'>
    <link href='https://fonts.googleapis.com/css?                        family=Lobster+Two:400,400italic,700,700italic' rel='stylesheet' 
          type='text/css'>
    <link rel="stylesheet" href="css/main.css">
    <link rel="stylesheet" href="css/responsive.css">
    <meta name="viewport" content="width=device-width. initial-scale=1.0">
  </head>
  <body>
    <header>
      <a href="index.html" id="logo">
        <h1>Zack Adams</h1>
        <h2>Developer</h2>
      </a>
      <nav>
        <ul>
          <li><a href="index.html" class="selected">Portfolio</a></li>
          <li><a href="about.html">About</a></li>
          <li><a href="contact.html">Contact</a></li>
        </ul>
      </nav>
    </header>
    <div id="wrapper">
      <section>
        <ul id="ImageGallery">
          <li>
            <a href="img/numbers-01.jpg">
              <img src="img/numbers-01.jpg" alt="Experimentation with color and texture.">
            </a>
          </li>
          <li>
            <a href="img/numbers-02.jpg">
              <img src="img/numbers-02.jpg" alt="Playing with blending modes in Photoshop.">
            </a>
          </li>
          <li>
            <a href="img/numbers-06.jpg">
              <img src="img/numbers-06.jpg" alt="Trying to create an 80's style of glows.">
            </a>
          </li>
          <li>
            <a href="img/numbers-09.jpg">
              <img src="img/numbers-09.jpg" alt="Drips created using Photoshop brushes.">
            </a>
          </li>
          <li>
            <a href="img/numbers-12.jpg">
              <img src="img/numbers-12.jpg" alt="Creating shapes using repetition.">
            </a>
          </li>
        </ul>
        <script src="http://code.jquery.com/jquery-1.11.0.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="js/app.js" type="text/javascript" charset="utf-8"></script>
      </section>
      <footer>
        <a href="http://linkedin.com/in/adamszs"><img src="img/linkedin.png" alt="LinkedIn Logo"  class="social-icon"></a>
        <a href="http://facebook.com/zackhary.adams.75"><img src="img/facebook-wrap.png" alt="Facebook Logo" class="social-icon"></a>
        <p>&copy; 2015 Zack Adams.</p>
      </footer>
    </div>

  </body>
</html>

CSS:

/**********************************
GENERAL
***********************************/
... (CSS code unchanged)
/**********************************
HEADING
***********************************/
... (CSS code unchanged)
/**********************************
NAVIGATION
***********************************/
... (CSS code unchanged)
/**********************************
FOOTER
***********************************/
... (CSS code unchanged)
/**********************************
PAGE PORTFOLIO
***********************************/
... (CSS code unchanged)
/**********************************
PAGE ABOUT
***********************************/
... (CSS code unchanged)
/**********************************
PAGE CONTACT
***********************************/
... (CSS code unchanged)
/**********************************
COLORS
***********************************/
... (CSS code unchanged)

/*******************
Overlay for LightBox
********************/
#overlay {
  background:rgba(0,0,0,0.7);
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  display:none;
  text-align:center;

}

#overlay img {
 margin-top:10%;
 max-width:70%;
 max-height:30%;
 display:none;
}

#overlay p {
font-size:10px;
 color:#ffffff; 
  display:none;

}

JS:

//Problem: User when clicking on image goes to a dead end
//Solution: Create an overlay with the large image - Lightbox

//declared variables
var $overlay = $('<div id="overlay"> </div>');
var $image = $("<img>");
var $caption = $("<p></p>");
var $presentation = false;

//appended html elements after the overlay
$overlay.append($image);
$overlay.append($caption);
//append the overlay to the body so it will take up the whole screen
$("body").append($overlay);

//capture the click event on an image
$("#ImageGallery a").click(function(event) {
  //set presentation to true
  $presentation = true;
  //if true
  if($presentation === true)
  {
  //prevent default image location
  event.preventDefault();

  var imageLocation = $(this).attr("href");
  $image.attr("src", imageLocation); 

    $overlay.show();
      var captionText = $(this).children("img").attr("alt");
      $caption.text(captionText);
      $image.fadeIn("slow");
      $caption.fadeIn("slow");
      presentation = false;
 }//end if statement

});
//show the overlay
//if the overlay is clicked
$overlay.click(function(){
      $presentation = false;
      $overlay.hide();
      $caption.fadeOut("fast");
      $image.fadeOut("fast");

});
//update the overlay with the image linked in the link
//get childs alt attribute and set caption

Answer №1

It seems like there might be a better approach to add markup here. You could consider including the overlay markup directly in the HTML file or using jQuery's .append() method for insertion instead.

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

Passing function parameters by value in JavaScript

Dealing with value and reference This method is functional: var temp = ['aaa','bbb']; var b = []; b[1] = { text: temp[1], onclick: function(){alert(temp[1])}}; // aaa on clock b[0] = { text: temp[0], onclick: function(){alert(temp[0]) ...

Strange margins surrounding the entire website

Recently, I've noticed an unusual issue while working on web development projects, whether it be using WordPress or simple HTML - a mysterious whitespace border surrounds the entire site. Even after checking the code and removing unnecessary elements, ...

Utilize JQuery UI dialog to display a confirmation dialog and return either true or false based

My plan is to submit a form only after validating it properly. To achieve this, I will first use an ajax post request to check for any duplicate values in the form. If duplicates are found, the ajax call will return HTML code; otherwise, it will return &ap ...

Tips for passing multiple values with the same key in Axios as parameters

I need to develop a function that allows users to select multiple categories and subcategories, then send their corresponding ids as a query string using Axios. For example, if a user selects category IDs 1 and 2, along with subcategory IDs 31 and 65, the ...

Rendering numerous partials using Haml layout

How can I properly indent the code? The structure of the files is as follows: = render :partial => "shared/head" = yield = render :partial => "shared/footer" Content of app/views/shared/_head.html.haml: !!!XML !!!1.1 %html{"xml:lang" => "pl", ...

I don't use Angular to execute a function when (load) is triggered

Whenever I try to open the div, the function doesn't seem to work correctly. Sample HTML: <div class="row table-dark table-bordered"> <div class="col-xl-12" (click)="isCollapsed=!isCollapsed;"> Click Me! <ng-container *ngIf= ...

Can we set $_SESSION equal to $_POST data?

I'm not sure if I'm on the right track, but let's consider this scenario. form.php <?php session_start(); $_SESSION['average'] = $num; echo ' <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"> <h ...

Ways to verify if the fetch variable contains data

function Login() { const [email, setEmail] = useState(""); const [password, setpassword] = useState(""); const [users, setUser] = useState([]); function login() { fetch( "http://116.202.231.219:8069/Restaurant/Login_Updated?Cont4=" + ...

Build a customizable digital clock script with Javascript/jQuery that displays the time in 24-hour format

Currently seeking a 24-hour format digital clock script in JavaScript/jQuery that also includes the date. The unique feature of this clock is that it will display images as its numbers and date, making for a visually appealing design. Additionally, I wou ...

Encountering an error when attempting to access undefined property while using a method as a callback

Exploring OOP and angular is new to me. I am currently trying to implement a reusable table with pagination that triggers an API request when the page changes (pagination within the table component). The issue arises when I attempt to access my method usi ...

Some elements in the Javascript variable list obj.names are null while others maintain values

I'm currently working on compiling a list from an array, but I've encountered an issue where some of the fields are null, causing the code to break and return: obj.names is null Below is the snippet of code that's causing the problem: My ...

Error: Unable to find the view "subscribers/index" in the views directory while running the mocha/chai test code

I am encountering the following error: Error: Failed to lookup view "subscribers/index" in views directory Here is the structure of my directories: +---controllers | coursesController.js | errorController.js | homeController.js | ...

Configuring relative file paths within the folders in tsconfig.json

In my project, I have set up a root folder named "TypescriptProgramming" which contains the tsconfig.json file. Inside this root folder, there is another folder called "chapter1" that further includes a folder named "minfunction". The minfunction folder ho ...

Discovering applied styles based on component props in Material-UI v5 using browser developer tools

When working with Material UI v4, I found it easy to identify which styles are applied by component props classname using browser dev tools. This allowed me to override specific styles of the component effortlessly. However, in Material UI v5, I am unsure ...

Tips for distinguishing individual submit buttons within a foreach loop

Here is my code snippet: <?php require_once 'core/init.php'; if($result = $db->query ("SELECT * from countries")) { if($count = $result->num_rows) { $rows = $result->fetch_all(MYSQLI_ASSOC); } ...

Exploring the power of Typescript alongside Styled Components and Material UI

Working with Typescript in conjunction with MUI and Styled-Components may lead to the need to pass props directly to MUI elements to address type errors... const Index = () => { return ( <StyledButton variant="contained" > ...

Display different text on a webpage based on login status

I am looking to display certain buttons on a webpage based on the user's login status in an MVC view page. To achieve this, I am using an Ajax function to check the user's login status and then show or hide the button accordingly. However, sinc ...

Progress bar using jQuery inside a list of tree folders in CSS

I have a folder tree structure and I would like to include a progress bar alongside each folder name, which is also a clickable link. The progress bar will be created using jQuery UI. The pblabel element should appear on top of the progress bar. The bar e ...

Regular expressions can be used to identify words within a block of text while ignoring any embedded HTML tags

As I explore the depths of regex, I stumbled upon a fascinating concept on stackoverflow. It involves enclosing specific words within a paragraph in span tags to enable users to click on them for further information. The implementation is flawless; however ...

Are you sure all of your statements have been successful? This is the problem

Implement a selection sort algorithm that swaps values at a given position with the minimum value index. Use the swap and indexOfMinimum functions provided below. Despite following the logic, there seems to be an issue preventing the code from successful ...