Interact with multiple divs using Jquery .click

I have created a single page with multiple anchors, each containing unique content that pops up when clicked. The functionality is working well, but I am facing an issue where clicking on a different anchor repeats the same content as the first anchor. I want each anchor to display its corresponding content in the popup. Below you will find my code. Thank you for your help.

<script>
$(document).ready(function() {
$('a.bio, a.bio2').click(function() {

// Fetching the variable's value from a link 

var loginBox = $(this).attr('href');

// Display the popup

$(loginBox).fadeIn(300);

// Set the alignment padding and border for center see css style

var popMargTop = ($(loginBox).height() + 24) / 2; 
var popMargLeft = ($(loginBox).width() + 24) / 2; 
$(loginBox).css({ 
'margin-top' : -popMargTop,
'margin-left' : -popMargLeft
});

// Adding the mask to body
$('body').append('<div id="mask"></div>');
$('#mask').fadeIn(300);
return false;
});

// Closing the popup when clicking on the close button or the mask layer

$('a.close, #mask').live('click', function() { 
$('#mask , .login-popup').fadeOut(300 , function() {
$('#mask').remove();  
}); 
return false;
});
});
</script>

The HTML

<a class="bio" href="#login-box">READ BIO &gt;&gt;</a>
     <div class="login-popup" id="login-box">
          <div id="popupimage">
          <h2>This is image 1</h2>
          </div></div>

<a class="bio2" href="#login-box">READ BIO &gt;&gt;</a></div>
     <div class="login-popup" id="login-box">
          <div id="popupimage">
          <h2>This is image 2</h2>
          </div></div>

The CSS

.login-popup {
background: none repeat scroll 0 0 #585858;
display: none;
float: left;
font-size: 1.2em;
height: 350px;
left: 50%;
padding: 20px;
position: fixed;
top: 50%;
width: 500px;
z-index: 9999999999;
color:#FFF;
}

Answer №1

In an HTML document, it is important to have unique ids for elements. In this case, there are two divs with the same id of "login-box." To resolve this, you should assign a unique id to each one:

<a class="bio" href="#login-box-1">READ BIO &gt;&gt;</a>
     <div class="login-popup" id="login-box-1">
          <div id="popupimage-2">
          <h2>This is image 1</h2>
          </div></div>

<a class="bio2" href="#login-box-2">READ BIO &gt;&gt;</a></div>
     <div class="login-popup" id="login-box-2">
          <div id="popupimage-2">
          <h2>This is image 2</h2>
          </div></div>

Answer №2

Ensure each content element has a unique identifier. Sharing the same id will result in only one being displayed when the code is executed.

To resolve this issue, modify the id of one of the content elements to make them distinct from each other.

Answer №3

There seems to be an issue in your code where both div elements have the same id, login-box.

To resolve this problem, you should modify the id of each element as shown below:

<a class="bio" href="#login-box-1">READ BIO &gt;&gt;</a>
     <div class="login-popup" id="login-box1">
          <div id="popupimage-2">
          <h2>This is image 1</h2>
          </div>
     </div>

<a class="bio2" href="#login-box-2">READ BIO &gt;&gt;</a>
     <div class="login-popup" id="login-box22">
          <div id="popupimage-2">
          <h2>This is image 2</h2>
          </div>
     </div>

Answer №4

Your HTML code needs to be updated for better functionality. Remember, classes can be repeated multiple times whereas IDs should only appear once.

  • Replace class="bio" and class="bio2" with id="bio" and id="bio2" respectively.
  • Convert the IDs "login-box" and "popup-image" to classes instead.

When defining a function for a 'click' event in your script, add an if statement to check for IDs bio or bio2.

$('.link').on('click', function() {
    if ($(this).attr('id') === 'bio') {
        // Perform specific action
    } else if ($(this).attr('id') === 'bio2') {
        // Do something different
    }
}

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

Steps for Loading the Icomoon FontIs this web text just

I'm having trouble getting Icomoon fonts to display on my website. I downloaded the files and placed them in my /fonts directory, following the steps outlined here. I edited icomoon/style.css with the appropriate font-face code: @font-face { font ...

Press the LI element to toggle the visibility of the UL element

I'm attempting to create a toggle navigation menu, but I'm having trouble identifying the issue with my code. It's a bit messy because I'm using a CMS that automatically generates the IDs and classes for the list menu. Ideally, I would ...

Embedding HTML within a PHP conditional statement

I recently encountered an issue while trying to display a source image using PHP. The initial code worked perfectly: <html> <h3>First Test</h3> <img src="example1.php" /> </html> However, I wanted to implement user validatio ...

HTML snippet as implemented in Python

As someone who is new to Python and CGI, I have a question that may seem simple. I want Python to generate a block of HTML whenever a visitor loads a page. <html> <body> <!-- Beautiful website with great content --> <!-- Suddenly... ...

Achieving consistent height for Grid items in Material-UI

I'm looking to achieve equal heights for these grid items without distorting them. Here's the current layout: https://i.sstatic.net/6dPed.jpg This is how I would like it to look: https://i.sstatic.net/BJZuf.jpg My challenge is that adjusting ...

Challenges with label and textbox alignment and sizing in Bootstrap 4

I'm currently working on a form based on Bootstrap and facing some challenges with positioning elements correctly. My goal is to create a form with labels/prompts and textboxes. However, I encountered the following issues in my code: The label colum ...

What could be causing the CSS and HTML connection to fail?

I am currently working with Flask on the Atom editor to build a website. Despite specifying the correct path for the CSS file in the link, every time I load the page, it appears without any styling. I have attempted changing the paths multiple times with n ...

Exploring ways to customize the input color of Material UI TextField when it is disabled [Version: 5.0.8]

I am having trouble changing the border color and text color when an input is disabled. I have tried multiple variations, as seen below: const textFieldStyle = { '& label': { color: darkMode?'#1976d2':'', } ...

The getImageData function is returning undefined RGB values

I am trying to create a feature where by clicking on an image, I can retrieve the RGB values of that specific pixel. Below is the code snippet I have implemented: <html> <body> <canvas id="kartina" width="500" height="500"></canvas&g ...

Issues with updating table data when clicking the "Remove Selected" button a second time in Angular 8

Whenever I click the "Remove Selected" button on my table, the selected rows with checkboxes should be deleted. The issue is that it works perfectly fine the first time I click the button, but if I repeat the same steps (click the checkboxes and then click ...

What key element is not included in this code?

I've been trying to create an interactive green circle that switches between red and green when clicked. Despite checking and rechecking my code, it's still not functioning correctly. I must be making a beginner mistake somewhere. Can anyone poin ...

Transferring data from local storage to a PHP server using AJAX

My attempt to transfer data from local storage to PHP using AJAX resulted in an error mentioning 'undefined index data'. chart.php <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <script type="t ...

Tips for importing font files from the node_module directory (specifically otf files)

We cannot seem to retrieve the fonts file from the node module and are encountering this error message. Can't find 'assets/fonts/roman.woff2' in 'C:\Projects\GIT2\newbusinessapp\projects\newbusinessapp\src ...

Step-by-step guide on repairing a countdown clock using JavaScript, HTML, and

I'm having issues with my JS on my website. I am new to this and currently in the process of setting up a website. I want to add a timer to show when the site will be active. It seems like there is an error somewhere in the JS code that I can't ...

Developing a Chessboard Using JavaScript

Seeking help with a Javascript chessboard project. I have successfully created the board itself, but facing difficulty assigning appropriate classes (black or white) to each square. Managed to assign classes for the first row, struggling with the remainin ...

How can I create a layout with cards that are arranged differently on wide screens compared to mobile using bootstrap?

Having trouble with my bootstrap 5.x layout. Can anyone help me achieve a design where the cards display like the left image on desktop/wide screens and like the right image on smaller width mobile screens? I currently have the mobile view showing A-B-C-D- ...

Utilizing jQuery for replicating form values specific to each column

Previously, I have utilized jQuery to transfer billing addresses to shipping addresses. However, when dynamically generating form rows with various PHP values, how can the form be configured so that upon checking a box, the recommended quantity of an item ...

Implement Spacing Between Components in React Native

Seeking assistance in resolving the issue of overlapping views within my react native app, specifically requiring spacing between them. Upon clicking the plus sign twice in the top right corner, the two Views overlap without any space between them (referr ...

Executing Python script via AJAX or jQuery

I need to execute Python scripts from JavaScript. The goal is to provide an input String to the Python script as an argument and then showcase the output on our webpage. Below is the Python code that runs smoothly on my Linux box: ./sample.py 12345 produc ...

Erasing a comment creates empty spaces

I've encountered an issue with my idea whiteboard where removing comments leaves unwanted blank spaces between ideas. For example, if I have the following comments: But when I delete something: Is there a way to ensure that no gaps are left between ...