What are the steps to extracting the data from <td> elements within a gridview control using JavaScript?

I am seeking assistance with some JavaScript-related issues. I have been using the gridview control to display comments retrieved from a database. My goal is to highlight the background of each comment when the user hovers over it, display a specific div element with a close button, and extract the text value of a label control within that div during the mouseover event. Additionally, I aim to show this extracted text value in another div element and retrieve the text value of a different label control located outside the gridview during the same onmouseover event. The details are outlined below:

HTML Layout:

<asp:Label ID="Label4" runat="server" Text=' <%#Eval("ID") %>'></asp:Label> //I want to get the text value of this control

<div id="test"></div> //here I want to put the text value of the name label control after getting it during onmouseover

    <div id="Username" style =" margin-left :100px; width :1000px">

 <asp:GridView ID="gvParentGrid" runat="server" Width="395px"
AutoGenerateColumns="false"  GridLines="None" BorderStyle="Solid" BorderWidth="0px"
            BorderColor="White" DataKeyNames="ID" onrowcommand="gvParentGrid_RowCommand"
            onrowdatabound="gvParentGrid_RowDataBound" >

<Columns>
<asp:TemplateField >
<ItemTemplate>

 <tr >

     <td id ="comment" onmouseover="highlightBG(this, '#C0C0C0');highlightNext(this, 'black')" onmouseout="highlightBG(this, 'white');highlightClear()" class ="highlightab" style ="border-bottom :2px solid Blue;border-bottom-color :Gray; border-left :0px; border-left-color :White; border-right :0px; border-right-color :White; border-top :0px; border-top-color :White;background-color :White;border-bottom :2px solid Blue;border-bottom-color :Gray; border-left :0px; border-left-color :White; border-right :0px; border-right-color :White; border-top :0px; border-top-color :White;background-color :White; height :100px; width :395px; margin-bottom :5px">
          <div id ="Close" style="display :none" ><asp:Button ID="Button3" runat="server" Text="X" style =" cursor:pointer; margin-left :365px; border:0px; background-color :White; color :blue; font-weight :bold; " /></div>
            <br />
            <asp:Image ID="Image1" runat="server" style="  margin-right :5px; background-image :url('Image/imagebackground.png');"  ImageAlign ="Left" Height ="60px" Width="60px" />
            <asp:Label ID ="ComID" runat ="server" style="display :none" Text =' <%#Eval("ID") %>' />
            <asp:Label ID="name" runat="server" style="font-weight :bolder; color :Blue; "  Text='<%# Eval("Name")%>' ></asp:Label> // I want to get this value diplay it in the div test
            <p id ="content" class="minimize" style =" border-radius: 4px 4px 4px 4px; max-width :395px; min-height :5px; margin-top :5px; margin-bottom :5px; margin-left :65px; display :block; background-color: #CCCCFF;"> <%# DataBinder.Eval(Container.DataItem,"Comments").ToString() %> </p>
           <a href="JavaScript:divexpandcollapse('div<%# Eval("ID") %>');" style ="margin-left :65px; margin-top :1px" >
             <input id="btndiv<%# Eval("ID") %>" type="button" value="Reply" style ="border:0px; background-color :White; color :blue; cursor :pointer " />
          </a>

     </td>

 </tr>

</ItemTemplate>
</asp:TemplateField>

</Columns>

</asp:GridView>

</div>

Javascript:

function highlightBG(element, color) {

element.style.backgroundColor = color;
var getval = document.getElementById("commentor").innerHTML;
document.getElementById("test").innerHTML = getval;
document.getElementById("Close").style.display ="block";

}

Summary of the issues I need assistance with.

  1. I require the Close div to appear in every row of the gridview upon hover. Currently, the code only displays the Close div in the first row, and it should transfer to other rows when hovering over them.

  2. I need to extract the text value of the name label inside the gridview's div and display it in the div test when hovered over.

  3. I also need to retrieve the text value of Label4, located outside the gridview, during the onmouseover event.

Thank you for any assistance provided. I hope to resolve these 3 issues soon.

Answer №1

Wow, this seems pretty complex. Maybe I should ask for a visual aid. Haha. Here's my understanding:

First and foremost, it's not advisable to have multiple elements with the same id. Change (id="Close") to (class="Close").

Using jQuery would be ideal for this type of DOM manipulation.

1)

function highlightNext(){
var highlightNext = document.getElementsByClassName('highlightNext');
    for(var i = 0; i < highlightNext.length; i++){
        highlightNext[i].style.display = 'none';
    }
}

2)

function highlightBG(element, color) {

    element.style.backgroundColor = color;
    //Instead of querying for "Close"
    var Close = element.children[0];
    Close.style.display = "block";
    var getval = Close.innerHTML;
    document.getElementById("test").innerHTML = getval;


}

3) Update the label to assign a static id

<asp:Label ID="Label4" runat="server" Text='<%# Eval("ID") %>' ClientIDMode="Static"></asp:Label>

Fetch the label using the information you already have:

document.getElementById("Label4");

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

The browser does not automatically set the Cookie

Trying to login involves making an API call using a POST HTTP request. post( postLogin(email), JSON.stringify({password: passwd}), { headers: { "Content-Type":"application/json" }, credentials: 'include' // also attempted with &a ...

Retrieving JSON information stored in a JavaScript variable

I'm feeling a bit embarrassed to admit it, but I am still learning the ropes when it comes to Javascript development. I've hit a roadblock and could really use some help from the experts here. Thank you in advance for all the assistance this comm ...

How can I access data entries in Firebase that have a particular key?

I've got some data stored in firebase with a structure like this: "application": { "companies": { "firebase": { "creation": { "name": "Firebase Inc", "location": "USA" }, "google": { "creattion": { ...

Is there a way to dynamically assign the Authorize attribute for Users based on data from a dbcontext (e.g. Authorize(Users = dbcontext.Users...) in an MVC application?

I am currently developing a controller where I want to establish an attribute on an action within the controller to validate users based on their username rather than their role. However, I do not wish to manually list out the names of each authorized user ...

Simply looking to activate keyup, keydown, or space events using jQuery/JavaScript

What is the method to activate a keyup event? Imagine having a text field with the id "item" and triggering an event with a space should result in adding a space to the text field. I am interested in triggering this event solely from the JavaScript consol ...

Troubleshooting: Angular version 4.3 Interceptor malfunctioning

I have been trying to implement new interceptors in Angular 4.3 to set the authorization header for all requests, but it doesn't seem to be working. I placed a breakpoint inside the interceptor's 'intercept' method, but the browser didn ...

The backdrop takes precedence over all other elements

The issue I'm facing is that the other Div is not appearing above the background. My goal is to have the background fill the entire screen. <title>The Ultimate Cube World Resource!</title> <link href="style.css" rel="stylesheet" type=" ...

Make sure to include all enum type values within the function's body to ensure comprehensive coverage

I am defining an enumeration called ApiFunctions with values like "HIDE", "SET_READ_ONLY", and "DESCRIPTION". Also, I have a type ValueOfApiFunction that should include all values of ApiFunctions. Additionally, I have a logic that listens for messages on ...

How can I create a floating thumbnail gallery that displays extra content when hovered over? Is there a way to make the hovered thumbnail overlap its neighboring

I am currently working on a gallery of thumbnail images. Each image is accompanied by a title below it: <div class='gallery'> <div class='thumb'> <div class='thumbimage'></div> <div class ...

What is the best way to extract a specific value from a line of data using JavaScript (JSON)?

My current task involves extracting the "correctAnswers" from a specific number. Let's take a look at this JSON example: { "questions": [ { "number": 3, "question": "☀️ ➕ ...

Is it better to define the SVG `viewbox` once in a `symbol`, or each time an SVG is `used`?

Based on my research, some tutorials suggest that when inserting an SVG <symbol> with <use>, the viewBox property only needs to be defined once - in the <symbol> tag. While this method seems to work fine, it results in the contents of th ...

An error occurred due to a missing optional section definition within nested layout pages

In my project, there is a _Layout.cshtml file where I have the line: @RenderSection("Scripts", required: false) Then, I have another file called StorageLayout.cshtml which utilizes _Layout.cshtml as its layout file. In this StorageLayout.cshtml ...

Leveraging Jquery for Enhanced Bootstrap Functionality in Angular 2

Recently diving into NG2, I've been experimenting with adding Slide Up/Down effects to the default Bootstrap 4 Dropdown component. The Bootstrap dropdown component comes equipped with two jQuery Listeners, $(el).on('show.bs.dropdown') and $ ...

The Challenge of AJAX and Postback Interactions

I have spent the entire weekend struggling with unwanted postbacks. The popup I am working on contains a button that triggers several stored procedures and then closes the popup window. My goal is to achieve the same result when users click the X close bu ...

The browser returns a 404 error for the Web API POST request, but it is successful when made using PostMan and Swagger

I encountered a peculiar issue with my Web API: it returns a 404 error when called from AJAX, but functions perfectly fine on PostMan and Swagger. Here is the post method code snippet: // POST api/<controller> [HttpPost] public int Post(string url, ...

Assign values from an array in jQuery to a <span> element

I am working with the following HTML code: <input type='radio' class='at-radio' name='re_widget[0][set_style_for_widget]' value='topbanner' /> <span class='at-radio-label'>topbanner</span> ...

The Bulma dropdown menu fails to display its items

Currently, I am working with Bulma as my CSS framework. While trying to implement the hamburger menu, I encountered some difficulties. The documentation did not provide clear instructions on how the functionality should work. Despite observing the menu tra ...

Executing a service prior to the loading of Angular 7 applications or components

Currently, I am in the process of developing an application using Angular 7. So far, everything is running smoothly as I have successfully managed API calls, JWT Token authentication with C#, and updating LocalStorage when needed during user login and logo ...

In JavaScript ES6, the const keyword can unexpectedly be altered even though it is intended to be constant

While experimenting with ES6, I came across an interesting feature - const values cannot be changed or reassigned. However, I noticed that in certain scenarios, it seems like we can change them. Can someone explain this? { const name = 'unchangabl ...

Modifying elements in AngularJS

Hello there! I'm fairly new to Angularjs and recently I've been working on implementing an edit feature for a field and attempting to save the data. Below is the HTML code snippet where the magic happens: <div class="row" ng-if="showme==&apos ...