Update Table Row Background Color in Separate Table by Clicking Button Using JQuery

Two tables are involved in this scenario - one that receives dynamically added rows, and another that stores the data to be included.

The illustration above displays these tables. Upon clicking the Edit button, the information from the selected row in Table 1 is transferred to the fields directly below it (Table 2), while the edited row in Table 1 is highlighted with CSS.

var row = $(a).parent().parent();
row.children('td').css('background-color', '#FFFFCC');

If I am able to determine the index of the row being edited in Table 1, how can I change its background back to white when the user clicks the 'Cancel' button?

Answer №1

Have you considered simply attaching an event to the 'onclick' function for the cancel link?

Here is an example:

$('.edit').on('click', function(event){
     var row = $(this).parent().parent();
     row.children('td').css('background-color', '#FFFFCC');
     //---Add event listener for cancel button.
     $('.cancel').once('click', function(event){
            row.children('td').css('background-color', '#FFFFFF'); //---Previous color     
     });

});

I have not tested this code, but I hope it provides a helpful solution or alternative for you.

Answer №2

When arranging rows in a table, the index refers to the position of the row and tbl represents the table being referenced.

let index = 1;
tbl = $("table")
$("#cancel").click(function(){
    let rows = $('tr', tbl);
    rows.eq(index).css("background-color", "white");
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table border=1 style="background-color:yellow">
  <tr><td>a</td><td>b</td></tr>
  <tr><td>c</td><td>d</td></tr>
</table>

<input type="button" id="cancel" value="cancel" />

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

I was caught off guard by the unusual way an event was used when I passed another parameter alongside it

One interesting thing I have is an event onClick that is defined in one place: <Button onClick={onClickAddTopics(e,dataid)} variant="fab" mini color="primary" aria-label="Add" className={classes.button}> <AddIcon /> & ...

Establishing a Next.js API endpoint at the root level

I have a webpage located at URL root, which has been developed using React. Now, I am looking to create an API endpoint on the root as well. `http://localhost:3000/` > directs to the React page `http://localhost:3000/foo` > leads to the Next API end ...

Enhancing webpack configuration with chainWebpack to customize infrastructure logging settings

I am working on the chainWebpack section within my vue.config.js. chainWebpack: config => { // Add "node_modules" alias config.resolve.alias .set('node_modules', path.join(__dirname, './node_modules')); ...

scalable and circular picture displayed within a bootstrap-5 card

Is there a way to resize and round the image within a bootstrap-5 card? I am unsure of how to achieve this using either bootstrap-5 or css3. My goal is to have the image appear smaller and rounded in the center of the card. Sample Code: < ...

ASP.NET Data Access Layer utilizes the Repository Pattern along with a Service Layer for efficient

I am diving into setting up the caching system for my ASP.NET MVC site, but I am struggling to find the perfect place for data caching. Currently, my architecture is structured like this: Controller -> Service Layer -> Repository. The repository ut ...

Tips for capturing the current terminal content upon keypress detection?

After successfully installing the terminal on a test page, I am looking to highlight matching parentheses within it. This is similar to what is done in this example: I have a working solution in place and now need to integrate it with the terminal. ...

Issues with iFrame Alignment

I recently created a digital catalog using FlipHTML5 and realized it needed to be more mobile-friendly. To address this issue, I included the following CSS: .size { width: 85vw; height: 75vh; Although this adjustment made the catalog display correctly on ...

Issues encountered when trying to use default color classes in Tailwind CSS

I'm currently working on a React project that utilizes the Tailwind CSS Framework. To integrate Tailwind into my React app, I used NPM to install it in the following way: npm install -D tailwindcss postcss autoprefixer npx tailwindcss init -p After i ...

Is there a way to monitor and trigger a function in jQuery when a loaded PHP file is modified?

I am currently working on a dynamic dashboard that automatically updates every few seconds to display new information fetched from a PHP file. My goal is to trigger an alert only when there is a change in the data itself, rather than just a refresh. In ord ...

Show various columns in Select2

I am currently utilizing select2 and I am interested in displaying a multicolumn table as a dropdown. In order to achieve this, the width of the drop-down container should differ (be larger) than the input itself. Is it feasible to accomplish this? Furth ...

Show a specific div using jQuery fadeIn() when the user reaches the top of an HTML section

The code below has a specific purpose: 1) Determine the current scroll position. 2) Locate the parent article and determine its offsetTop for each .popup_next which represents a section on the site. 3) Calculate an offset value by adding 30px to the off ...

Connecting Next.js to a Database: A Step-by-Step Guide

I am interested in developing an application similar to Samsung Health that will involve heavy querying on a database. I am unsure whether it would be more beneficial to create a custom server using Node.js (with Express.js) instead of using the integrate ...

Tips for vertically aligning <p> and <div> elements without using the table-cell property

I am trying to align my text vertically in the middle using a ch-grid <div> (the circle color element). Is it possible to achieve this without specifying a display attribute for the <p> element? I plan to hide it by setting display:none initia ...

Creating a PNG image on a canvas, converting it to a data URL, and then transmitting it using an XMLHttpRequest in NodeJS/Express

I've been experimenting with different methods found on Google, but so far none have worked for me. I'm currently working on implementing the signature-pad, a JavaScript/HTML5 canvas solution for eSignatures. My goal is to save the canvas data as ...

Ways to create a unique animation for reducing the width of a div in a specific direction?

Currently, I have managed to animate the decrease in width of a div element. However, it is currently decreasing from right to left, and I would like it to decrease from left to right instead. Can someone please provide guidance on how to achieve this? ...

I encountered an issue while attempting to connect to my MySQL database using my Express API endpoint: error message "connect ECONNREFUSED 127.0

I am currently in the process of developing a web application for a bootcamp using Express and MySQL. I have set up a route to handle a GET request to an endpoint which is supposed to query my MySQL database table and retrieve all records. My intention is ...

What would be more efficient: inputting all items in a form at once or adding them one by one consecutively

My mind is preoccupied with a dilemma: is it better to have all possible items already on the form, or should items only be added when the user requests them? Let me elaborate - Imagine I have a form with 4 input fields and one textarea. Alongside that, t ...

Assistance required in adjusting the clickable area of the link

While casually exploring a random website, I decided to experiment with some design elements. However, I encountered an issue where the link was only clickable on the left side and not the right. This is how it appears: There is a div containing a heading ...

The conclusion of Ajax's response signals the beginning of Jqmobi

I am currently utilizing the jqMobi framework for a web application. Within my application, I establish a connection to the server, send the username and password, and in return, receive a string similar to this: [{"utoken":"e43aa84cc304a1ed3722832616294 ...

Response is sent by Sequelize Foreach loop before data is updated

My goal is to retrieve all content and media from a post, then append it to a new post before sending it as a response for easier access to the data. The issue I'm encountering is that the response is being sent before the content and media are fetche ...