Is it possible to modify an element within a list based on the identifier of its containing div?

I have created some additional software for my project, which includes a large div with smaller divs inside. Here is an example of how it is structured:

<div class="scroll-area" id="lista">

        <div class="box" id="item1">
            <p>Item #1</p>
            <p class="info"><a href="#" id="lnkInfo">Info</p></a>
            <p class="info"><a href="#">Take</p></a>
        </div>

        <div class="box" id="item2">
            <p>Item #2</p>
            <p class="info"><a href="#">Info</p></a>
            <p class="info"><a href="#">Reservar</p></a>
        </div>

        <div class="box" id="item3">
            <p>Item #3</p>
            <p class="info"><a href="#">Info</p></a>
            <p class="info"><a href="#">Take</p></a>
        </div>

        <div class="box" id="item4">
            <p>Lab #4</p>
            <p class="info"><a href="#">Info</p></a>
            <p class="info"><a href="#">Take</p></a>
        </div>

        <div class="box" id="item5">
            <p>Item #5</p>
            <p class="info"><a href="#">Info</p></a>
            <p class="info"><a href="#">Take</p></a>
        </div>

        <div class="box" id="item6">
            <p>Item #6</p>
            <p class="info"><a href="#">Info</p></a>
            <p class="info"><a href="#">Take</p></a>
        </div>
               </div>

Using pop-up for items:

 <div class="popUp hide" id="popUp">
        <div class="stylePopUp">
          <span>Info</span>
          <span value="Close" id="btnClose">x</span>
        </div>
            <ul>
        <li>Name: Item #1</li> !-- This is where I want to replace the information -->
            <li>BLA</li> 
        <li>BLA</li>
        <li>BLA</li>
    </ul>



        </div>  

CSS Styles:

.box {
   display: inline-block;
   padding: 2px;
   width: 75px;
   border: 2px solid black;
   margin: 3px;
}


.box:hover{
border:2px solid #e67e22;
}

Javascript Functions:

var elementInfo = document.getElementById('lnkInfo'),
elementBtnClose = document.getElementById('btnClose'); 

elementoVerInfo.addEventListener('click', function () {
displayPopUp('popUpCorrect');
});

elementoBotonCerrar.addEventListener('click', function () {
hidePopUp('popUpCorrect');
});

function displayPopUp(pIdDivToShow){
var fElementDivToShow = document.getElementById(pIdDivToShow),
newClass ='';
newClass = fElementDivToShow.className.replace('hide','');
fElementDivToShow.className = newClass + ' show';
}

function hidePopUp(pIdDivToShow){
var fElementDivToShow = document.getElementById(pIdDivToShow),
newClass ='';
newClass = fElementDivToShow.className.replace('show','');
fElementDivToShow.className = newClass + ' hide';
}

Currently, when I click on "Info," a pop-up displays with information like:

Name: Item X

Instead of creating a div for each item, I would like to dynamically generate this information using JavaScript based on the clicked div. I am unsure about the correct syntax and approach in JavaScript for achieving this task.

I was thinking of implementing a loop that compares the IDs of each div and assigns a value to "Name:" X accordingly. However, my limited experience in programming makes me uncertain. Any guidance or assistance on how to proceed would be highly appreciated.

Thank you for your help! Best Regards!

Answer №1

As per your request, here is the code snippet in Jquery. Feel free to check out the attached Fiddle for a live example:

     $('.box').click(function(e){
        var divobj = $(this);
        alert(divobj.attr('id'));

      });

http://jsfiddle.net/AmarnathRShenoy/aJt2K/

If you want a more efficient solution, consider mixing jquery and vanilla JS.

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

Modify the dropdown content upon selection

On my page, users are required to select a product using drop down filtering with four levels: Category, SubCategory, Product, and Variant. However, it can be cumbersome for users to navigate through each drop down to get to the final variant. I am looki ...

What is the significance of the message "Legacy octal literals are prohibited in strict mode" in the context of ReactJS?

I am encountering an issue while attempting to iterate through these arrays where I keep receiving the error message "Legacy octal literals are not allowed in strict mode." const myList =[ { id: 01, title: 'FrontEnd Engineer&apo ...

Using Express.js with synchronous functions can cause the web application to freeze

While developing an app using Express, I realized that I made a mistake regarding my usage of Promises. Here is the code in module.js: module.exports = function(arguments){ for(let k =1; k < something.length; k++){ let options = { & ...

The CSS formatting is not being properly applied within the innerHTML

I have a scenario where I am trying to display a Bootstrap card using innerHTML in my TS file, but the styles are not being applied to this content. I suspect that the issue might be because the styles are loaded before the component displays the card, cau ...

JavaScript method overloading involves defining multiple functions with the same name

In my JavaScript code, I have implemented method overloading using the following approach: function somefunction() { //1st function } function somefunction(a) { //2nd function } function somefunction(a,b) { //3rd function } somefunction(); // ...

Graphics distorting on android devices when implementing createjs and canvas technology

I have a question regarding the performance of my game that I'm developing to work on both desktop browsers and mobile devices. Using HTML5 and CreateJs, most of the game is being drawn on a canvas element. While everything runs smoothly on a standar ...

A guide on accessing information from a post form using an express.js server

Issue: Whenever the client submits a form using a post request to the server, the express server receives an empty body (req.body = {}). Objective: My goal is to retrieve req.body.username and req.body.password on a post request from the client (using the ...

The fieldset css in PrimeNG differs from the website's original design

On my website, the appearance of the fieldset can be seen here: https://i.stack.imgur.com/TTS8s.jpg. I did not make any CSS changes that altered the fieldset. I am utilizing primeNG v7 and Angular 7. <p-fieldset legend="Toggleable" [toggleable]="true" ...

Tips for modifying the property value of a navigation bar in Bootstrap using Sass

I currently have a navigation bar on my webpage that is styled with the following CSS properties: .navbar { position: relative; display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; padding: 0.5rem 1r ...

Creating specialized paths for API - URL handlers to manage nested resources

When working with two resources, employees and employee groups, I aim to create a structured URL format as follows: GET /employees List employees. GET /employees/123 Get employee 123. GET /employees/groups List employee groups. GET /employees/groups/123 ...

Parsing dynamic parameters or slugs from the URL within the app directory using Next.js

I'm facing an issue with Dynamic Routing where I can't seem to access the parameter. Every time I try to read it, I encounter errors such as: TypeError: Cannot destructure property 'themenID' of 'router.query' as it is undef ...

Developing with Phonegap Build: A Guided Process

With all the conflicting information available, I am seeking clarity on this topic. Objective: To create and enhance a Phonegap app using Phonegap Build. 1) My preference is to utilize Phonegap Build without needing to install Android and iOS SDKs. 2) I ...

Close pop-up upon successful AJAX response in ASP.NET MVC

I have a modal in my view that allows me to create a new record in the database. The view for this functionality is contained within a partial view. Below is the code for the view: <script src="~/Scripts/jquery-3.1.1.js"></script> To han ...

Choose from a range of knockout fetching options for the select feature and choose one

Upon loading my page, there is a <select> element positioned. I am utilizing knockout data-bind to retrieve the values like this: <select data-bind="attr: { id: 'input-' + id(), required: !allowBlank }, option ...

How can we send a JSONArray through a dynamically generated button event and then retrieve the elements of the JSONArray?

In the following SCRIPT code snippet: var jsonArr = [ { " name": "John", "age": 31, "city": "New York" }, { "name": "Smith", "age": 25, "city": "Dubai" } ]; function create() { createButton(jsonArr); } function cre ...

Having trouble with testing axios web service promises to return results using Jest in a Vue.js application

In the process of developing a new application with Vue.js and axios, I am focusing on retrieving stock market details based on company names. To kickstart the application, I am compiling all the names of US-based S&p 500 companies into a JavaScript ar ...

Entry module could not be located due to an unresolved 'babel-loader' error

As a newbie to the world of React, I found myself facing an issue while setting up the installation and loading all the necessary dependencies. Upon running the npm start command, I encountered the following error message: ERROR in Entry module not found: ...

Angular: module instantiation unsuccessful

Just getting started with Angular and running into an issue where the module seems to be unavailable. https://docs.angularjs.org/error/$injector/nomod?p0=plopApp My code is very basic at the moment, just setting things up: @section scripts{ <script s ...

What is the purpose of adding "/dist" to the library import statement?

Currently, I am in the process of developing a react component library using vite as my primary build tool. After successfully compiling the project and deploying it to the npm registry, I encountered an issue when importing it into my client app. Specifi ...

Creating a personalized gradient using Tailwind CSS and Daisy UI framework

I’m attempting to incorporate a unique gradient into my next.js application with the help of Tailwind CSS and Daisy UI. Below is the specific code snippet I plan on using: background: linear-gradient(180deg, rgba(192, 192, 192, 0.04) 0%, rgba(201, 180, ...