Implementing a JQuery function to generate a popup whenever a user clicks on a table row (tr) in an

I am working on a JSP page that contains a table, and I want to implement a pop-up window functionality when clicking on a specific row in the table. I have attempted to use JavaScript to connect with the row but so far, I have not been successful in creating the desired pop-up.

Included below is an example code snippet from a JSFiddle I created. Currently, my code includes a Java for loop that generates each table row dynamically based on information retrieved from a database.

<tr OnClick="display('test');">
      <td><strong>showSpeed</strong></td>
      <td>15</td>
      <td>The speed of the show/reveal</td>
    </tr>

 <script>function display(test) {
        //code to display a pop-up goes here
    }</script>

JSFiddle link: https://jsfiddle.net/y2y1w24L/1/

Thank you for your help!

Answer №1

If you want to create a stylish popup, consider using JQuery UI. By combining your code with the example provided on http://jqueryui.com/dialog/, you can achieve a visually appealing result.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js></script>
    <!--<link rel="stylesheet" href="/resources/demos/style.css">-->
</head>
<body>
    <script>
        function display(test) {
            $("#dialog").dialog();
        }
    </script>
    <table>
        <tr onclick="display('test');">
            <td><strong>showSpeed</strong></td>
            <td>15</td>
            <td>The speed of the show/reveal</td>
        </tr>
    </table>

    <div id="dialog" title="Basic dialog" style="display:none">
        <p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
    </div>
</body>
</html>

Another alternative is to explore bootstrap modals available at: http://getbootstrap.com/javascript/#modals

Answer №2

When utilizing the "display" function, event.target allows you to retrieve a reference to the element that was clicked by the user:

function display(selection) {
    alert(event.target.outerHTML);
}

Visit this link for more information.

This reference can be used to showcase the appropriate popup content.

Answer №3

Here is a code snippet that could be helpful for you:

var PopUP=document.createElement("div");
PopUP.className="PopUP";
PopUP.innerHTML="<div id='mainDivBack' style='display:block;'><div id='PopUpDiv' style='display:block;'>"+" Welcome"+ "<input type='button' value='x' id='btnClose' style='display:block;' onclick='popClose();'/></div></div>";
document.body.appendChild(PopUP); 

function popClose() {
    document.getElementById("btnClose").style.display = 'none';
    document.getElementById("PopUpDiv").style.display = 'none';
    document.getElementById("mainDivBack").style.display = 'none';

}

function display(test) {
    PopUP.innerHTML="<div id='mainDivBack' style='display:block;'><div id='PopUpDiv' style='display:block;'>"+test+ "<input type='button' value='x' id='btnClose' style='display:block;' onclick='popClose();'/></div></div>";

    document.getElementById("btnClose").style.display = 'block';
    document.getElementById("PopUpDiv").style.display = 'block';
    document.getElementById("mainDivBack").style.display = 'block';
}

Below is the CSS styling for this code:

{ background: none repeat scroll 0 0 #dd2904; border: 1px solid #c13031; color: white; font-size: 18px; font-weight: bold; margin: 5px; padding: 0 1px 4px; position: absolute; right: 10px; top: 0; } #btnClose:hover { background: none repeat scroll 0 0 #ff4565; }
#PopUpDiv
{
    background: none repeat scroll 0 0 lightgray;
    border: 1px solid gray;
    border-radius: 8px;
    box-shadow: 1px 1px 12px 3px white;
    font-family: "Lucida Console" ,arial;
    height: 70px;
    padding: 35px;
    position: absolute;
    width: 300px;
}
#mainDivBack
{
    background: radial-gradient(lightgray, transparent) repeat scroll 0 0 transparent;
    height: 100%;
    left: 0;
    padding: 20% 25%;
    position: fixed;
    top: 0;
    width: 100%;
}
.Grid
{
    background-color: #fff;
    border: 1px solid #525252;
    border-collapse: collapse;
    color: #474747;
    float: left;
    font-family: Calibri;
    width: 99%;
}

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

utilize the Aspose library in Java to seamlessly convert HTML files into PowerPoint (

I have a Java variable that contains HTML code. I want to utilize the Aspose library to execute and render this HTML code into a PowerPoint presentation, with the CSS reference included. It would be great if the resulting PowerPoint is editable. ...

What is the best way to choose an HTML element in React?

Is there a way in React to change the theme value based on localStorage and set it to either light or dark mode? https://i.sstatic.net/Iecsj.jpg I am familiar with using Ref hooks in components, but how can I access a DOM element in the index.html file? ...

sending data from a callback to an express router

As I embark on learning node.js, I've encountered a challenging issue. In my passportAuth.js file, I create a user and have a callback to ensure the user is created successfully. The code snippet looks something like this: req.tmpPassport = {}; var ...

Picture goes missing from slideshow

I am currently using a CSS + Javascript slideshow on my website, inspired by an example from the W3Schools website. Here is the code I have adapted for my web application: $(document).ready(function() { var slideIndex = 1; function showSlides(n) { ...

Typescript is struggling to locate a module that was specified in the "paths" configuration

Within my React project, I have set up a module alias in the webpack config. Now, I am looking to transition to Typescript. // I have tried to simplify the setup as much as possible Here is my tsconfig.json located in the root directory: { "compilerOp ...

Select a random option from the dropdown menu

I have the following script: <script> function $(id) { return document.getElementById(id); } function getImage() { $('loader').src='/misc/images/loading.gif'; var url = "http://mouse ...

What are some methods to secure my API keys within my React application?

What steps can I take to secure my api keys in my react application? Should I incorporate something with express? My goal is to avoid creating any server-side components to handle the API calls. Currently, my backend is managed by firebase but I also uti ...

Having trouble with your computed includes not working in Vue.js? Unsure of how to fix it?

How come when I use "includes" in Vue with my v-model, Vue.js logs an error? However, if I write (.includes("blabla)), everything works fine. What could be the issue? Also, how can I return the entire array if the (if) condition will work? For example, ...

Utilize AJAX to connect to a remote domain from an extension

I am currently working on creating a Chrome extension that will utilize AJAX to fetch data from a specific webpage and then generate notifications based on the content of that page. The webpage I am targeting is a ticketing system, and my goal is to deter ...

A unique and dynamic design concept for my website: a zigzagging structure

I am trying to achieve a layout similar to the one at the provided link, with variable sized div tags on the left and right edges. Can someone please assist me in styling the left and right sides accordingly? Key styles I have used: .rightside { margin-l ...

Using CSS to enforce a specific height for text by overflowing its parent container horizontally

I have a lengthy phrase that takes up too much space on mobile devices. Here is an example: .artificial-phone-viewport { width: 320px; height: 500px; border: 1px solid darkgrey; } .container { width: 100%; height: 100%; } .text { /* * Do ...

Steps for creating a fixed menu bar that remains stationary while scrolling

I am facing three challenges: When I hover over my menu links, I want them to become 'bold', but it causes the items on the right to move slightly. How can I prevent this movement? In regard to the dropdown menu on "Two", how can I ensure t ...

What is the best method for properly inserting images into a Vue Carousel 3D slider?

I'm currently exploring the Vue Carousel 3D documentation and I am having trouble displaying a single image on each slide. Let me elaborate: I aim to create a slider similar to the one shown in the Controls Customized example in the documentation. Th ...

I'm encountering an issue where the database table in Postgres is not updating correctly while working with Node

Currently, I am working on a CRUD application where I can successfully read and delete data from the database table. However, I have encountered an issue when trying to update specific data for a particular route. Every time I attempt to update the data in ...

Is there a way to utilize Javascript/JQuery to access and interpret PHP-generated HTML form data in a multi-dimensional array format?

I am having difficulty in reading and manipulating data from an HTML form using Javascript/JQuery. Situation: I have a sales screen displaying multiple products, and I aim to iterate through them all, analyze their contents, and showcase some of the data ...

Tips for implementing visual alterations using dynamically generated HTML scripts

When I dynamically generate HTML code from markdown, I typically wrap it in the <p></p> tag like so: <p class="embedded_markdown"> ... <h1>...</h1> ... <h3>...</h3> ... </p> I decided to experiment with sho ...

Enclosing Material UI's DataGrid GridActionsCellItem within a custom wrapper component triggers a visual glitch if showInMenu is enabled

Here is how my MUI DataGrid columns are structured: const columns = [ { field: "name", type: "string" }, { field: "actions", type: "actions", width: 80, getActions: (params) => [ ...

What is the best way to display a modal once an Ajax call is successful

Despite my efforts, I am encountering errors instead of the expected modal message after successfully adding an article. Here is the code I have so far: Modal: <div id="myModal" class="modal fade"> <div class="modal-dialog modal-confirm"> ...

What is the best way to replicate the Ctrl+A action on an element using jQuery or JavaScript?

Is there a way to trigger the Ctrl+A key combination in a textarea element when a specific event occurs, without resorting to caret positioning or selecting all text separately? I'm looking for a method that simulates hitting Ctrl+A using a cross-brow ...

Receiving Null Value Upon Asynchronous API Call Before Data Retrieval

Struggling with Fetching and Displaying API Data in a Table I am facing a challenge where I need to fetch an API multiple times and populate the data into a table. The issue arises when the data for a specific year is not available, causing the table to b ...