Displaying a div on the click of an image within a for loop using JavaScript

Currently, my website has a blog page that only showcases each individual blog post's photo and title. This is achieved through the use of a FOR loop (shown below). I am interested in implementing a feature where clicking on a blog post's photo opens up a DIV displaying a short excerpt and the blog title. Unfortunately, I am feeling quite out of my depth with this task. Any assistance would be greatly appreciated!

{% for article in blog.articles %}
{% unless article.tags contains 'exclude' and current_tags == blank %}
<div class="grid__item large--one-third small--one-whole {% cycle 'one-blog', 'two-blog', 'three-blog' %}" id="article-{{ article.id }}" data-alpha="{{ article.title }}" style="margin-bottom:50px;">   
  <div class="article-info"> 
    <div class="article-info-inner">
      <a href="{{ article.url }}"> <img src="{{ article.image.src | img_url: 'large' }}"></a>
      <span class="post-excerpt" style="    font-size: 0.8em;  margin-bottom: 0.8em;  display: block; margin-top: 1em;">
        {{article.excerpt | strip_html | truncatewords: 30}}
      </span>
      <h2 class="blog_title" style="text-align: center;"><a href="{{ article.url }}">{{ article.title }}</a></h2>
    </div>
  </div>
</div>
{% endunless %}
{% endfor %} 

Answer №1

To incorporate hidden content behind images, simply nest a div within a container:

<div> the container
 <img> the image
<div style="display:none">Hidden text</div> the hidden content
</div>

Next, implement some JavaScript code:

<script>
window.onload=function(){
  var images=document.getElementsByTagName("img");
  for(var i=0;i<images.length;i++){
   img=images[i];
   img.onclick=function(){
     this.parentNode.childNodes[1].style.display="block";
   };
   }
 };
</script>

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

What is the best way to include a select HTML element as an argument in an onSubmit form function call?

I am currently facing an issue where I am attempting to pass HTML elements of a form through the submit function as parameters. I have been able to successfully retrieve the nameInput element using #nameInput, but when trying to access the select element ( ...

How to precisely position an element using jQuery .wrap method

I am in the process of developing two jQuery functions to enclose gravity form inputs within Bootstrap prepend and append input-group HTML elements. I am attempting to manage the placement of the input HTML content that I am surrounding with the new marku ...

Enhancing Website Design with Image Borders

I'm currently designing a website for a Media Agency and I'm looking to add a Frame-style border that gives the impression of being placed inside a television. The Television Image I have is a PNG file with a solid outer frame and a transparent i ...

Extract JSON Data from Nested Object

I'm currently struggling with parsing JSON data from an object within an object using a Node.js script. Here's the JSON Object: { "Item":{ "job_change_request":"task0020764", "id":"a156fc4e-e8d4-424f-a792-0c8cf8e3ca46", ...

Checking and rectifying website addresses with JavaScript

Summary: User inputs a short-form/alias URL into a field (website.com). I need to identify the full URL and replace the input with it (which could be www.website.com or www2.website.com based on redirection). -- My web service allows users to input URLs. ...

Tips for accessing the data from an object's num property using the name property as a selector

I am attempting to locate the value of the num property within this object by using the name property to identify the correct object from the hashtagsl array; thus, obtaining the correct num value. I need to accomplish this task with only the information o ...

Enhancing jQuery Component while making an Ajax request

When a user clicks a button, I am making an ajax call to send out multiple emails. My goal is to update a "Please wait..." div before and after the call with status updates, as well as report any errors that may occur. However, I have encountered an issue ...

Tips for Verifying Checkbox in Angular 5

When attempting to validate the checkboxes in a form with different fields, I encounter an issue. The HTML code for the form is as follows: <div class="form-group"> <label class="login_label">Courses</label> <span style="col ...

Retrieve the recordset value in order to automatically mark a checkbox as selected

I am having an issue with a checkbox named "cbBatch" on one of my pages. After checking the box and submitting the form, "cbBatch" is stored as 1. On the following page, I want cbBatch to be automatically checked if the value in the corresponding database ...

Tips for sending an array to an action method in ASP.Net MVC

Currently, I am working on an ASP.Net MVC5 project where I am attempting to send JSON data to the controller's action method via an AJAX request. The JSON data consists of various form values. To facilitate a master-detail form structure with one-to-m ...

What is the method for positioning the cursor at the beginning of a center-aligned placeholder within a contenteditable element?

Here is the code snippet I am working with: HTML: <div class="title" contenteditable="true" placeholder="Title"></div> CSS: .title { width: 500px; margin: 0 auto; text-align: center; } [contenteditable=true]:empty:before{ conte ...

What causes the small black line to appear on the right side of an image when it is turned into a link?

When I turn an image into a link, why does it always seem to display a tiny black line to the right of the image? ...

Determine the state of the checkbox property in HTML

Is there a way to conditionally set the checked property of a checkbox? In my application, I have checkboxes for each row. The database includes a column 'IsSaved' with values of either 0 or 1. If 'IsSaved' is 1 for a particular row, I ...

The extent of locally declared variables within a Vue component

Within this code snippet: <template> <div> <p v-for="prop in receivedPropsLocal" :key="prop.id" > {{prop}} </p> </div> </template> <script> export default ...

Conditional Rendering with Next.js for Smaller Displays

I've implemented a custom hook to dynamically render different elements on the webpage depending on the screen size. However, I've noticed that there is a slight delay during rendering due to the useEffect hook. The conditionally rendered element ...

CSS - text featuring a gentle shadow hue and a deeper foreground color

I'm encountering an aesthetic issue where my list of texts have random light/bright colors, making them hard to read on white backgrounds. I've tried setting the text color to shadow, but now I want the text itself to be a darker, more readable c ...

Evaluate different styles that are activated by hovering the mouse over certain elements

One of my buttons is supposed to change styles when the mouse moves over it: background-color: green; &:hover { background-color: red; } This is how I am testing it: fireEvent.mouseOver(button); expect(button).toHaveStyle(` background-color: re ...

jQuery Ajax Redirect Form

I am currently developing an HTML application with a form. Upon clicking the submit button, I initiate a server-side call using jquery.ajax(). However, when the server returns an exception, such as a Status Code 500, I need to display an error message on t ...

Struggle with comparing strings in different cases

When utilizing the "WithText" function within a Testcafe script, it appears to be case-sensitive. How can I modify it to be case-insensitive? For example, allowing both "Myname" and "myname" for a user input. It's problematic when a script fails due t ...

What is the best way to display div table data in the correct order on mobile devices using Bootstrap 3?

Currently, I am utilizing Bootstrap3 and have implemented the code below. While it displays correctly on my desktop, the issue arises when viewed on a mobile device as the text is not grouped properly. My desired mobile view should look like this: Heade ...