I have successfully implemented a click effect on one image and now I wish to apply the same effect to the remaining images

I've exhausted all my options and still can't crack this puzzle. Let me break it down for you:

Upon entering the page, users are greeted with a collection of images each tagged with classes (for example, img01 and img02). When an image is clicked, it stays in place (img01's z-index increases), while the rest fade away (a white fill DIV covers img02), accompanied by text detailing the piece (text fades in from the object-text tag associated with img01).

Although I managed to get the functionality working for img01, replicating the same for

img02</code has proved tricky. Additionally, I'm looking to introduce more tags like <code>img03
and img04 and exploring if there's a more efficient way to structure this.

If you need a reference for the functionality, here's the link: http://jsfiddle.net/kenhimself/nvwzgus0/4/. The HTML, CSS, and JavaScript code can be found below.

Thank you in advance!

HTML

<a href="#" id="object" class="img01"> <img class="img01" src="http://media.tumblr.com/tumblr_mcepyv1Qfv1ru82ue.jpg"/></a>
<div id="object-text" class="img01">
    <h1>img01 Text<br/>img01 Text</h1>
</div>

<a href="#" id="object" class="img02"> <img class="img02" src="http://media.tumblr.com/tumblr_mcepyv1Qfv1ru82ue.jpg"/></a>
<div id="object-text" class="img02">
    <h1>img02 Text<br/>img02 Text</h1>
</div>

<div id="filler"></div>

CSS

html, body {
    width:100%;
    height:100%;
}
#object {
    top: 100px;
    left:100px;
}
#object-text {
    display:none;
    z-index:100000;
    bottom: 0;
    left: 0;
}
#filler {
    display:none;
    width:100%;
    height:100%;
    position:fixed;
    background-color: white;
    z-index:1000;
    opacity: 0.8;
}
h1 {
    font-size:20px;
    font-family: sans-serif;
    font-weight: 100;
    font-style: normal;
    color: red;
}

.img01, .img02 {
    position:absolute;
}
.img01 img, .img02 img {
    width:200px;
    height:auto;
}
.img01 {
    top: 20px;
    left: 20px;
}
.img02 {
    top: 20px;
    right: 20px;
}

Javascript

$("#object").click(function (e) {
    e.stopPropagation();
});

$("#object").click(function (e) {
    e.preventDefault();
    e.stopPropagation();
    $("#object").css("z-index", "2000");
    $("#object-text").fadeIn("slow");
    $("#filler").fadeIn("slow");
    $("#inner").css("z-index", "2000");
});

$(document).click(function () {
    $("#filler").fadeOut("slow");
    $("#object-text").fadeOut("slow");
});

Answer №1

It appears that there are some inconsistencies in your code. For a more effective approach, consider assigning unique IDs to each DOM element and targeting images by their class names. I have made adjustments to your example, reorganized it slightly, and provided a better method for implementation.

http://jsfiddle.net/abc123/8/

I have enclosed each image in a container tag, eliminated duplicate IDs, and utilized class names instead.

<a href="#" class="image img01">
    <img class="img01" src="http://example.com/img01.jpg"/>
    <div class="object-description">
        <h1>Image 01 Description<br/>Image 01 Description</h1>
    </div>
</a>

<a href="#" class="image img02">
    <img class="img02" src="http://example.com/img02.jpg"/>
    <div class="object-description">
        <h1>Image 02 Description<br/>Image 02 Description</h1>
    </div>
</a>

<div id="spacer"></div>

A CSS class has been added to manage the z-index dynamically, simplifying the process of toggling it on and off.

a.top {
    z-index: 2000;
}

Event handling has been adjusted to target the new container tag:

$("a.image").click(function (e) {
    e.preventDefault();
    e.stopPropagation();
    $(this).addClass("top");
    $(this).find(".object-description").fadeIn("slow");
    $("#spacer").fadeIn("slow");
});

The method for resetting the z-index of images has been modified:

$(document).click(function () {
    $("#spacer").fadeOut("slow", function() {
        $("a.image").removeClass("top");
    });
    $(".object-description").fadeOut("slow");
});

Answer №2

One critical issue that stands out is the presence of two objects sharing the same id. Rectifying this will resolve the error in your code. I suggest converting what you currently have as ids (object) to classes, and what is currently designated as classes (img02 and img01) to ids.

Upon further examination of your code, it appears that repetitive occurrences of this mistake are widespread. It's imperative to adhere to the rule of never reusing ids while coding. Both your 'a's and 'divs' contain duplicate ids...

Please don't interpret this feedback as harsh criticism, but rather a call for significant improvements. Feel free to reach out with any questions or if additional assistance is required.

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

Switching the background image dynamically in Vue.js upon page access

My goal is to update the 'background-image' when the page is accessed. export default { created () { document.getElementsByTagName('html')[0].style.background = "url('../assets/background-blur.png') center" } } //Logi ...

Prevent postback when clicking on an image button in ASP

I have a project in ASP where I have an image button that allows users to browse an image and display it. I am utilizing a script for this project. Here is the code I am using: ASPX: <asp:Panel ID="stage" runat="server" cssClass="containment-wrapper" ...

Bootstrap progress bar loading does not function properly

I have a progress bar on my page with three parts: progress-bar-success, progress-bar-warning, and progress-bar-danger. I would like each part of the progress bar to complete sequentially, starting with progress-bar-success, then progress-bar-warning, and ...

Obtain input from request payload in WCF/ADO.NET Data Service

Why are my parameters getting lost when I try to post to an ADO.NET Data Service? This is what my code looks like: [WebInvoke(Method="POST")] public int MyMethod(int foo, string bar) {...} Here's how I'm making the ajax call using prototype.js ...

What is the best way to include an attribute to an object in a knockout observable array and ensure a notification is triggered?

Implementing Knockout.js in my project, I have an observable array included in the view model. function MyViewModel() { var self = this; this.getMoreInfo = function(thing){ var updatedThing = jQuery.extend(true, {}, thing); ...

How to update a <table> in AngularJS or JavaScript to keep it current

I've been trying for hours to figure out how to refresh the <table> with random values when a button is clicked. I've assigned id="myTable" to the <table> and tried using the .reload() function, but it's not working. Any suggesti ...

Creating a responsive design for mobile apps in Ionic using CSS

I need help with making my Ionic app responsive across all mobile devices. The design on the page was created using CSS, but it is not displaying properly on every device. How can I ensure that it adapts to different screen sizes? <template> <Io ...

Expanding the input range slider to fill the entire available space

https://jsfiddle.net/a5gdhmfn/1/ <div> <i>O</i> <input type="range" /> <button/> <div>...</div> </div> Inside a div, there is a range slider along with other fixed-width elements. The issue ...

Utilize the angularJS filter to emphasize the search text within the search results

I have a search box that filters results displayed on the screen. I am using a filter called 'startWith' for this purpose. Now, I need to implement a feature where the search text is highlighted among the search results in angularJS. For example ...

Show Struts2 error message on an HTML webpage

I have included error and message handling in my Struts2 action class using the addActionMessage and addActionError methods. I am now looking for code that will allow me to display these messages and errors on the HTML page. Is it feasible to do so? ...

swap out an element in an array with an extra element

My array contains elements with both id and des properties. I would like to add an additional property like value:0 to each object in the array. I achieved this using a loop. let data = [ { "id": 1001, "des": "aaa" }, { ...

Maximizing Efficiency: Utilizing a Single Panel across Multiple HTML Files with jQueryMobile

Can a panel defined in index.html be used on another page, such as results.html? Or do I have to define the panel on every page and duplicate the HTML code on each page? Because I want the panel to be consistent across all pages. This is the panel in my ...

Sending a Nan alert regarding a JSON attribute

I recently completed a project using Angular4 that involves connecting to a nodeExpressJS server to retrieve JSON data and update the object with new information. Below is the onSubmit function from the addemployeeform. onSubmit(formValue: any) { cons ...

Increment field(s) conditionally while also performing an upsert operation in MongoDB

I need to perform an insert/update operation (upsert) on a document. In the snippet below, there is a syntactical error, but this is what I am attempting to achieve: $inc: { {type=="profileCompletion"?"profileCompletion":"matchNotification"}: 1}, If the ...

Determine the optimal number of units to purchase with a given budget

Looking to automate a procurement process with my new agent: let perunitcost = 100; let agentMaxQty = 60 let agentMaxTotal = 5000 let qtyToBePurchased = 0 The agent has a budget limit of $5000 and can procure up to 60 items. In this scenario, the agent w ...

The API mistakenly inserts multiple entries instead of just one

Recently, I upgraded to mvc net core 3.0 from net core 2.2 and encountered a strange issue. Every time I attempt to add a new record to the database, the application ends up creating 9 copies of the same record in the DB. This occurs across all tables in m ...

What is the proper method for running a script using the Selenium JavascriptExecutor?

On my test.html page, I've included the following code snippet: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> </head> <body> ...

Error message indicating that the event "OnkeyDown" is not in sync within the React application

Hey everyone! I'm a beginner in web development and I'm struggling to understand why the event "OnKeyDown" is triggered the second time. My task involves changing (increasing, decreasing) parts of a date (day, month, year, hour, minutes, seconds) ...

Trigger a jQuery event when an element is moved to a new line

Is there a way to trigger a jQuery event when an element moves to a new line in the view? I want to hide navigation items and run additional JavaScript code when this happens. Any ideas on how to achieve this? Here's how my navigation appears in a wi ...

Trying to bring in components from directories above

I'm currently facing an issue with importing components from a parent directory into my project. My goal is to be able to use these components across multiple projects, which seems like the most straightforward approach. However, when I attempt this, ...