What is the best way to transfer a row value from one table to another and then reinsert it back into the original table

I attempted to transfer a row value from one table to another and then back to the original table, but unfortunately, I was unable to find a solution.

$('#one tbody tr td input.checkbox').click(function() {
  if ($(this).attr('checked')) {
    var row = $(this).closest('tr').clone();
    $('#two tbody').append(row);
    $(this).closest('tr').remove();
  }
});

$('#two tbody tr td input.checkbox').click(function() {
  if ($(this).attr('checked')) {
    var row = $(this).closest('tr').clone();
    $('#one tbody').append(row);
    $(this).closest('tr').remove();
  }
});

$('button').on('click', function() {
  $('*').removeAttr('style');
  $('#one tbody tr td input.checkbox:not(:checked)').parent().css('border', 'red 1px dashed');
});
table {
  margin: 10px;
}
td {
  padding: 5px;
  background: lightblue;
}
button {
  float: right;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<button>TEST SELECTOR!</button>

<table id="one">
  <tbody>
    <tr>
      <td>Stupid</td>
      <td>Flanders</td>
      <td></td>
    </tr>
    <tr>
      <td>Hi!</td>
      <td>There!</td>
      <td>
        <input type="checkbox" class="checkbox">
        <label>Check ME!</label>
      </td>
    </tr>
    <tr>
      <td>Ho!</td>
      <td>There!</td>
      <td>
        <input type="checkbox" class="checkbox">
        <label>Check ME!</label>
      </td>
    </tr>
    <tr>
      <td>Neighbor</td>
      <td>eno!</td>
      <td>
        <input type="checkbox" class="checkbox">
        <label>Check ME!</label>
      </td>
    </tr>
    <tr>
      <td>Okaly</td>
      <td>Dokaly</td>
      <td>
        <input type="checkbox" class="checkbox">
        <label>Check ME!</label>
      </td>
    </tr>
  </tbody>
</table>

<table id="two">
  <tbody>
    <tr>
      <td>Stupid</td>
      <td>Flanders</td>
      <td></td>
    </tr>
  </tbody>
</table>

fiddle

However, the current code only removes the checked row without returning it to the original table.

When I click on a row, it should be returned to the previous table.

Does anyone know how to properly move the row value back to its original location after transfer?

Any assistance in resolving this issue would be greatly appreciated.

Answer №1

After duplicating, adding, and completing the task again, you must utilize

$('body').on('click','#one tbody tr td input.checkbox', function(){})
$('body').on('click','#two tbody tr td input.checkbox', function(){})

for both #one and #two

VIEW DEMO

Answer №2

attempting:

 $('body').on('click','#one tbody tr td input.checkbox',function(){
        if( $(this).attr('checked')){
        var row = $(this).closest('tr').clone();
         $('#two tbody').append(row);
             $(this).closest('tr').remove();
        }

    });
    $('body').on('click','#two tbody tr td input.checkbox',function(){
        console.log('cc')
        if(!$(this).attr('checked')){
        var row = $(this).closest('tr').clone();
         $('#one tbody').append(row);
             $(this).closest('tr').remove();
        }

    });
    $('button').on('click', function(){
        $('*').removeAttr('style');
        $('#one tbody tr td input.checkbox:not(:checked)').parent().css('border','red 1px dashed');
    });

jsfiddle:http://jsfiddle.net/wGGDb/76/

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

What is the process for displaying data within an Angular 10 component?

As I embark on my journey with Angular 10, my goal is to display the current user in the profile.component.html and the navbar in app.component.html. Below you will find the relevant code snippets: users.ts export interface User { username : string ...

What is the best way to transform a JS const into TSX within a Next.js application?

Exploring the code in a captivating project on GitHub has been quite an adventure. The project, located at https://github.com/MaximeHeckel/linear-vaporwave-react-three-fiber, showcases a 3D next.js application that enables 3D rendering and animation of mes ...

Tips for passing a variable containing an image source from Node.js to a Jade file

Below is the code snippet from my index.js file, where I'm using express for routing: var express = require('express'); var router = express.Router(); /* GET home page. */ router.get('/', function(req, res){ var db = req.db; ...

Applying FAB (Material UI) to span across 2 columns in a CSS Grid: A step-by-step guide

Is there a way to make the zero button larger than the other buttons in its row by spanning 2 columns? I would like it to be an oversized oval shape. Appreciate any assistance you can provide. import "./styles.css"; import Button from '@mui/materia ...

JS backbone require global models in js

I am looking to implement a UserSession model that will handle loading and saving session IDs into cookies using the jQuery cookie plugin. Below is the code for my UserSession model module: define(['jQuery', 'Underscore', 'Backbo ...

Is it possible to utilize AngularJS ngAnimate to perform cross-fading transitions on list items?

I am currently exploring the ins and outs of angularJS with my simple message ticker example. This ticker displays messages by toggling the CSS display property of one of the li elements. <div id="ngtickerMessage" class="ngtickerMessage"> ...

Allowance for text overflow on subsequent line

Below is the snippet of HTML code I am working with: <h4 class="clickbelow">This is a very long line that overflows onto a new line when the width is small.</h4> Here is the CSS that I have implemented: .clickbelow:before { content: url( ...

Explaining the process of defining `this.props` and storing data in the global Redux state

I am currently diving into the world of react and Redux and I'm using a react project on GitHub called overcode/rovercode-ui as a learning tool for understanding react and Redux. As I explore this project, I have come across some intriguing questions. ...

Building a DIV Element in the Space Between a Header and Footer Using HTML and CSS

I'm attempting to get my DIV element (MainPageImage) to cover the entire screen area, situated between the header and the footer. Here's my HTML code: body { padding: 0; margin: 0; overflow-y: scroll; font-family: Aerial; font-size: ...

Dynamic rows in an Angular 2 Material data table

I'm currently working on dynamically adding rows to an Angular 2 Data Table ( https://material.angular.io/components/table/overview) by utilizing a service called "ListService". This service provides me with the columns ("meta.attributes") to be displ ...

Issue with React Native Hook where converting a function into a class variable results in 'undefined'

We are currently in the process of converting a react native function into a class so that we can implement state management compatible with Firebase's real-time database. It recently came to our attention that Hooks cannot be used within a class, so ...

What methods can be used to differentiate between value equality and reference equality when watching something?

In the world of AngularJS, the $watch function comes with an interesting twist - it has an optional third parameter. By default, this parameter is set to false, which means the watch will only trigger if the watched reference changes. But if you set it to ...

Can you identify the issue within this code that combines HTML5, CSS, and JavaScript?

I'm attempting to create a feature where, upon clicking a button, a hidden div will be revealed. However, my current implementation doesn't seem to be working. function reload(){ window.reload(); } function vis(x,z,a){ var xpar = docu ...

Is it possible to adjust the height of input fields in MUI Reactjs?

Having trouble adjusting the height of input textfields using in-line css. <FormControl sx={{ "& .MuiOutlinedInput-notchedOutline": { borderColor: "blue", }, "&.Mui-focused .MuiOutlinedInpu ...

The div element is persisting despite AJAX attempts to remove it

I am currently developing an application that allows users to post and comment. I have a situation where I need to delete a specific comment by clicking on the associated 'x' button. To achieve this, I am making an Ajax call to the remove-comme ...

Is there a way for me to duplicate a complex element multiple times within the same page?

As an illustration, let's say I have a social tab located in my header that I would like to duplicate in the footer. This tab is comprised of various buttons with SVG images on top, event listeners linked to button IDs, and CSS styling. One option is ...

Determine whether a div contains any child elements

I am working on a piece of code that checks for the presence of the class "wrong" in any of my divs, and if found, displays a jQuery UI dialog box. My goal now is to enhance this code by adding a condition where it also checks for empty divs before showing ...

A step-by-step guide on showcasing database data on an HTML page using jQuery

I am currently using jQuery to make an HTTP GET request and fetch JSON data from a database. After executing the request, I received the JSON array successfully. Now, I am attempting to display this JSON data on an HTML page by using the jQuery $(newData ...

Tips for successfully submitting a collection of items with unique identifiers to a controller in asp.net core using ajax

I am looking to send groups of radio buttons to the controller through AJAX. Here is a snippet from my view: @model IEnumerable<DataLayer.Entities.Questions.Question> @{ ViewData["Title"] = "ShowQuestions"; Layout = " ...

Utilizing NPM configuration variables across different operating systems (Windows and Linux): A comprehensive guide

When you use the syntax npm_package_config_<variable>, its usage varies depending on the operating system. In package.json (for Linux & Windows) config: { foo: "bar" } Then for usage: On Linux node app.js --arg=$npm_package_config_foo On Wi ...