What is the best way to transfer a row from one table to another using jQuery by selecting the row and then clicking to move it?

As a newcomer to stackoverflow, I am encountering an issue while working on two tables. There is a right button that should move the row into the next table, but unfortunately, my attempts to make it work have been unsuccessful.

I would greatly appreciate any assistance.

$(function() {
  $(document).on("click", "#submit", function() {
    var getSelectedRows = $(".src-table").parents("tr");
    $(".target-table tbody").append(getSelectedRows);
  })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<h3>
  Source table
</h3>
<table class="src-table">
  <tr>
    <th>Select</th>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Age</th>
  </tr>
  <tr>
    <td>Jill</td>
    <td>Smith</td>
    <td>50</td>
  </tr>
  <tr>
    <td>Eve</td>
    <td>Jackson</td>
    <td>94</td>
  </tr>
</table>
<br>
<input type="button" value="Submit" id="submit">

<h3>
  Target table
</h3>
<table class="target-table">
  <tr>
    <th>Select</th>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Age</th>
  </tr>
</table>

Answer №1

It appears that the issue stems from an incorrect selector, resulting in getSelectedRows containing an empty jQuery object.

Avoid using parents() altogether since the tr elements are children of the table being selected. Instead, simply use $(".src-table tr");, which should suffice.

Notably, I included a <thead /> element within the tables to prevent duplication of the header row. Give this a try:

$(function() {
  $(document).on("click", "#submit", function() {
    var getSelectedRows = $(".src-table tbody tr");
    $(".target-table tbody").append(getSelectedRows);
  })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h3>Source table</h3>
<table class="src-table">
  <thead>
    <tr>
      <th>Select</th>
      <th>Firstname</th>
      <th>Lastname</th>
      <th>Age</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Jill</td>
      <td>Smith</td>
      <td>50</td>
    </tr>
    <tr>
      <td>Eve</td>
      <td>Jackson</td>
      <td>94</td>
    </tr>
  </tbody>
</table>
<br>
<input type="button" value="Submit" id="submit">

<h3>Target table</h3>
<table class="target-table">
  <thead>
    <tr>
      <th>Select</th>
      <th>Firstname</th>
      <th>Lastname</th>
      <th>Age</th>
    </tr>
  </thead>
  <tbody></tbody>
</table>

Answer №2

One way to accomplish this is by selecting all TR elements using the jQuery selector $(".src-table tr") and then appending them to the target table. It's important to note that your function does not include a tbody element.

$(function() {
  $('#submit').on('click', function() {
    var getSelectedRows = $(".src-table tr");
    $(".target-table").append(getSelectedRows);
  })
});

Answer №3

Swapping

var getSelectedRows = $(".src-table").parents("tr");

for

var getSelectedRows = $(".src-table").find("tr");
did the trick for me.

Check out the jsFiddle

.parents() is used to target ancestor elements, while find() is ideal for descendant elements. Since in your code tr is a child of .src-table, you should use either $(".src-table").find("tr"); or $(".src-table tr");

When dealing with a jQuery object representing a group of DOM elements, the .parents() method enables us to explore through the ancestors of these elements in the DOM tree and create a new jQuery object from the matching elements ordered from closest parent to outer ones; the elements are returned sequentially from the immediate parent upwards. If there are multiple DOM elements in the original set, the resulting set will also be in reverse order of the original elements, removing any duplicates.

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

Steps for creating a herringbone design on an HTML canvas

Seeking Help to Create Herringbone Pattern Filled with Images on Canvas I am a beginner in canvas 2d drawing and need assistance with drawing mixed tiles in a cross pattern (Herringbone). var canvas = this.__canvas = new fabric.Canvas('canvas' ...

"My drop down button menus and background image seem to be experiencing some technical difficulties. Can someone help

I'm having trouble getting a drop-down menu to work with a reptile image as the background. When I remove the image, the drop-down works fine. I've checked for errors but can't seem to find any. Is it an issue with the code or maybe a typing ...

What is the best way to prevent Firefox from storing the data of a textarea in the local environment?

I have been developing a website locally, and I have noticed that there are numerous <textarea> elements present on the site. One issue I am facing is that whenever I reload the site, the content within the <textarea> remains the same. This pe ...

What steps should I follow to integrate jQuery file upload within webmatrix (ASP.NET Web Pages)?

Recently, I came across this jQuery file upload on https://github.com/blueimp/jQuery-File-Upload. It seems like a great tool to use for my website, but the documentation mentions that I will need to create my own file upload handler. Has anyone here used j ...

What is the best way to ensure that the CSS padding for a button matches the width of the column consistently?

I have encountered an issue where I am unable to maintain consistent padding for a button relative to the column width. This is crucial for me because the button's background changes when a user hovers over it. However, using a fixed value for the but ...

"Utilizing the flex box feature in IE 10 for efficient text trunc

.container { background: tomato; width: 100px; display: flex; } .text-left { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } .text-right { } <div class=container> <div class="text-left"> title title title ...

Disable the resizing function for the text area

I have a form in Rails that includes a text area. The issue is that the text area can be resized by the user, which I do not want. How can I prevent the user from resizing it? <%= form_tag "doit", :id => "doit_form" do -%> Names <br/& ...

trouble combining two arrays of JavaScript objects

I'm facing a challenge with an object array. I need to add a new object "WITH" a specific index, but I'm unsure of the correct approach. This is my current attempt: //ORIGINAL DATA: this.fetchedData = [{ "startMinute": 0, //Remove "annotati ...

Restrict the dimensions of the image to fit within the div

I've been struggling to resize my LinkedIn logo on my website. I've attempted various methods, like using inherit and setting max height and width to 100%, but the logo keeps displaying at full size. Do I need to use a different type of containe ...

The usage of the "this" keyword in a function call is not

When I try to call a local function using the this pointer, I encounter an error message stating 'Uncaught TypeError: undefined is not a function'. The issue arises on line this.createtimetable(); within the loadtimetable function. The relevant ...

Place an element that exceeds 100% in size at the center

How can I center an image that is larger than its parent element? I have set the min-width and min-height to 100% so that the picture will always fill up the parent element. The issue arises when the image does not match the proportions of the parent elem ...

Default Link Color Being Utilized by Blog Title

My website was created using a theme (HTML, CSS, JavaScript), but the Blog Title is stuck with the default link color. I've attempted several methods to change the Title color without success. What's the solution for changing the Title color? ...

jqgrid - dismiss the dialogue box

Currently, I am encountering an issue with jqgrid 4.3.1 while using form editing with local data. The problem lies in the form not closing after adding or editing data. Below is a snippet of my code: $('#studentset').jqGrid({ data: mydata, ...

What is the best way to synchronize the heights of two div containers when zooming in the browser causes one container to expand, ensuring they both remain aligned at the bottom

Greetings! I am currently working on designing a webpage that consists of two parts: a left section with buttons and a right section with a table view. Everything looks perfect until I zoom in my browser to 150% or more, causing the content in the table to ...

I'm looking for a way to implement pagination using Jquery AJAX in my PHP application. How

Currently, I am implementing pagination for my MySQL data using PHP. My aim is to utilize the jQuery load function in order to dynamically load the next page when a pagination link is clicked. The pagination links are structured as follows: <div class= ...

Overriding the width value of a computed CSS style with an inline CSS style is not possible

There's a peculiar issue that has me stumped. The webpage I'm currently working on is loaded with CSS styles. To customize the width of one of my div elements, I added inline CSS code specifying the exact width I want (and it looks correct in th ...

Date Selection Limited to Year and Month

I am struggling with a date range picker and I only want to display the year and month while selecting the start and end month. However, it is not working as expected. How can I adjust it to meet my requirements? Here is an example of the screen layout I ...

Utilizing ngx-bootstrap Modal for Data Transfer to Component

I am currently dealing with an array called drivers in my component. When a button is clicked in the component, it triggers a ngx-bootstrap modal to display the fullname value from the drivers array. Now, I am looking for a way to retrieve the selected nam ...

The processing indicator fails to function properly when trying to refresh a jQuery datatable

I am currently customizing the loading indicator for the datatable using a spinner called startLoadGlobal(SPINNER_DATA_TABLE_FINANCEIRO_CARREGAR_REGISTROS) in jQuery. It works correctly when I initially load the datatable, however, when I reload it, the sp ...

Using VB.NET with Selenium to locate and interact with dropdown elements

My knowledge of selenium is limited, and I'm facing difficulty in selecting an element from a dropdown menu on AliExpress using vb.net. The issue arises when the driver either fails to locate the intended element or finds another element with the same ...