Showing a pop-up on a click of a dynamically created table row, showcasing information specific to that row

I am facing a challenge with my dynamically generated table that is based on the JSON response from an AJAX call. What I am trying to achieve is to display additional data in a modal when a table row is clicked. This would be simple if the data was hard coded, but since the content on my page is generated dynamically, I am struggling to figure out how to pass the data to the modal.

So far, I have attempted to use .addAttribute and .appendChild to pass information to a single modal, but it seems like the modal remains blank. I suspect this is because the modal loads at the same time as the page and doesn't get populated even when the button is clicked.

Here is my modal structure:

<div id="modalDisplay" class="modal">
    <div id="modalDisplayContent" class="modal-content">
        <span class="close" onclick="onClickGetSpan()">&times;</span>
        <div id="formDisplayContent" class="modalContent">
        </div>
    </div>
</div>

Below is the function that is triggered when a table row is clicked:

function getSingleTask(id){
    var taskName = "";
    var taskDescription = "";
    $.ajax(
        {
            url: "http://localhost:8080/tasks/" + id,
            type: "GET",
            dataType: "JSON",
            success: function(){

                taskName = data.name;
                taskDescription = data.description;
                var nameTitle = document.createElement("H2");
                nameTitle.setAttribute("value", name);

                var description = document.createElement("P");
                description.setAttribute("value", taskDescription);

                document.getElementById("formDisplayContent").appendChild(nameTitle);
                document.getElementById("formDisplayContent").appendChild(description);
            },
            error: function () {
                var noNameTitle = document.createElement("H2");
                noNameTitle.setAttribute("value", "Error finding task! Try again!");
                document.getElementById("formDisplayContent").appendChild(noNameTitle);
            }
        }
    )

    var modal = document.getElementById("modalDisplay");
    modal.style.display = "block";

}

One possible solution I am considering is to create modals for each table row, sort them by IDs, and then open the corresponding modal when a table row is clicked. However, before I explore that option, I would appreciate any suggestions or improvements on my current approach. Thank you in advance.

Answer №1

This is my typical code snippet:

<div id="myModal" class="modal">
    <div id="modal-dialog" class="modal-content">
        <span class="btn"></span><br/>
        <span id="changethis"></span>
    </div>
</div>


function btnFromTableRows() {
    $.ajax( {
        url: "http://localhost:8080/main/",
        type: "GET",
        dataType: "JSON",
        success: function(){
            mydata = "<div class='container'>This is a sample container</div>";
            document.getElementById("changethis").innerHTML = mydata;
        },
        error: function () {
            mydata = "<h2>Invalid data</h2>";
            document.getElementById("changethis").innerHTML = mydata;
        }
    }
}

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

JavaScript library that provides SQL-like functionality for easily manipulating arrays

After successfully parsing a csv file into an array of objects, I find myself in need of making multiple alterations to that array. Currently, I am utilizing lodash methods for this task, but I am finding it to be quite verbose. I am considering working wi ...

Unable to populate an array with JSON elements within a for loop triggered by useEffect

In my code, there is an issue with the array candleRealTimeDataQueue not updating correctly. Below is the snippet of the problematic code: let candleCurrentJSONDataWS = null; var candleRealTimeDataQueue = []; let tempDateTime = null; let ca ...

Navigating through Router Parameters in VueJS

Currently, I am in the process of developing a blog using Vue.js and I am still learning the ropes. To explain briefly, I have a list of dynamic elements that are displayed on the screen. When a user clicks on one of these items, I want to navigate to the ...

Accessing PageController through XmlHttpRequest is not allowed in Shopware 6

I'm encountering an issue when attempting to send a request to my customized StorefrontController on the most recent version of Shopware 6.2.2. The error message I'm receiving is: PageController can't be requested via XmlHttpRequest. I&apos ...

Learn the method for toggling images in HTML while simultaneously storing a 0 or 1 in a flag variable

In my design, I would like to have a star icon that displays two star icons at once - one blank and the other yellow. These stars will swap their appearance when clicked or tapped on touch screen devices. Additionally, I want to be able to store values as ...

How to dynamically load a file based on the chosen option value in React

Two select textboxes on my page are named Choose City and Choose District. I also have some files related to cities and districts: // cities.js const cities = { "01": { "name": "City 1", "code": "01" }, ...

Tips for applying a linear gradient mask to div elements?

I'm attempting to create a fading effect on the edges of a div by using two overlay divs positioned absolutely on either side. My goal is to have the background of the page visible once the fade is complete, effectively masking the content of one div ...

The object function router(req, res, next) is encountering an error as it does not contain the required method for handling the request

I am trying to add a new row to my MySQL database, but I encountered an error message. Here is the scenario: I have set up Passport and hjs in my project. I am passing form data from app.js to a JavaScript file where I aim to insert the data. Object funct ...

Mystery Surrounding MP3 Playback in ASP.NET

Currently, I am utilizing an ASP.NET server side control to play MP3 files on my website. While I could utilize JavaScript or Flash controls for this purpose, the issue I'm facing is that I want the music to only play once (at the start of the site) a ...

Intermittent Z-index conflicts in need of a solution - seeking guidance

Is there a way to make the h1 heading and img image elements appear on top of the opaque div they are enclosed in? I want them to look unaffected by the transparency of their parent div. Here is a Fiddle demonstrating my issue: <div id="main"> ...

When executing `npm run start`, a blank page appears exclusively on the server

I recently set up a Vue landing page on my Mac. In the terminal, I navigated to the folder and executed "npm install" and "npm run dev", which ran without any issues. However, when trying to do the same on a managed server, I encountered challenges with t ...

Tips for effortlessly moving content by dragging and dropping it into a text box

Before attempting to create something, I want to verify its feasibility. Begin with a text area that can be pre-filled with text and allow users to add or delete text. Alongside the text area, there are small elements that can be images or HTML components ...

I am curious about this "normalize.less" that appears in the F12 Developer Console

Trying to track down information on this mysterious "normalize.less" that is appearing in the Chrome 76 developer console has proven to be a challenge for me. In Firefox 69's console, it displays bootstrap.min.css instead, leading me to believe that i ...

Troubleshooting Vue.js: Why is .bind(this) not behaving as anticipated?

Demo: https://codesandbox.io/s/23959y5wnp I have a function being passed down and I'm trying to rebind the this by using .bind(this) on the function. However, the data that is returned still refers to the original component. What could I be missing h ...

Customizing the appearance of Twitter Bootstrap based on the AngularJS input class $invalid

I'm struggling with getting error messages to display properly in my Bootstrap form when using AngularJS. I followed the instructions on this link: but I still can't figure out what I'm doing wrong. Any advice? Thanks <div class=" ...

Guide on obtaining an access token for an API through the use of the POST method in JavaScript

I am trying to obtain credentials for an API that uses OAuth2. The API documentation outlines the process as follows: Request access token: POST: auth/access_token Url Parms: grant_type : "client_credentials" client_id : Client id clien ...

Exploring jQuery.each: A guide to navigating JSON objects

As a beginner in working with JSON, I am struggling to iterate over a JSON response received via AJAX. My objective is to extract and loop through checkbox values retrieved from a database table such as 2,3,7,9,3. However, I am currently facing difficultie ...

Struggling with adding documents into mongoDB with the help of mongoose and node.js

I have a mongoose model defined below: module.exports = mongoose.model('vbDetail', { company_name: String, rowsdata: {vals: { date: Date, transaction_type: String, transaction_num: Str ...

Ways to center text in a div using vertical alignment

/* Styles for alignment */ .floatsidebar { height: 100%; float: left; overflow: hidden; width: 30px; line-height: 1.5; } .vertical-text { height: 100%; display: inline-block; white-space: nowrap; transform: translate(0, 100%) rotate(-90 ...

The Ajax Get Request functions properly when called in a browser, but returns a 302 error when executed within an iframe. What might be causing this discrepancy?

I am currently developing a web application that initiates multiple ajax requests upon startup. The app functions perfectly when executed independently in the browser. However, when I run it within an iframe, one of the ajax requests unexpectedly returns ...