Jquery/Javascript issue on checkbox checked add row from one table to another with input box manipulation

neeraj.sonar

I have a table on the say on the left side and it has four columns. I have another table on the right and I want two columns from the first column to be displayed on the right column. All I am able to achieve is adding whole new row, where as I want columns formatted in row. I know I have to append html using jquery but can't find a way.

html code below for left table:

<table class="table table-hover" id="tbl1">
                  <thead>
                    <tr>
                      <th>Items</th>
                      <th>Quantity/Calories</th>
                      <th>Portion</th>
                      <th>Approx. Callories</th>
                    </tr>
                  </thead>
                  <tbody>
                    <tr data-index="1">
                      <td><input type="checkbox" class="chkclass"><span> Apple</span></td>
                      <td>1 medium/65cal</td>
                      <td><input type="text" ng-model="qty"> Nos. </td>
                      <td><input type="text" class="uneditable-input" placeholder="aa"> cal.</td>
                    </tr>
                    <tr data-index="1">
                     <td><input type="checkbox" class="chkclass"><span> Banana</span></td>
                      <td>1 medium/90cal</td>
                      <td><input type="text"> Nos. </td>
                      <td><input type="text" class="uneditable-input" placeholder="aa"> cal.</td>
                    </tr data-index="1">
                    <tr>
                     <td><input type="checkbox" class="chkclass"><span> Grapes</span></td>
                      <td>30Nos./70cal</td>
                      <td><input type="text" ng-model="qty"> g </td>
                      <td><input type="text" class="uneditable-input" placeholder="aa"> cal.</td>
                    </tr>
                    <tr>
                     <td><input type="checkbox" class="chkclass"><span> Guava</span></td>
                      <td>1 medium/50cal</td>
                      <td><input type="text"> g </td>
                      <td><input type="text" class="uneditable-input" placeholder="aa"> cal.</td>
                    </tr>
                    <tr>
                     <td><input type="checkbox" class="chkclass"><span> Jackfruit</span></td>
                      <td>4 pieces/90cal</td>
                      <td><input type="text"> Nos. </td>
                      <td><input type="text" class="uneditable-input" placeholder="aa"> cal.</td>
                    </tr>
                      <tr>
                     <td><input type="checkbox"><span> Mango</span></td>
                      <td>1 medium/180cal</td>
                      <td><input type="text"> Nos. </td>
                      <td><input type="text" class="uneditable-input" placeholder="aa"> cal.</td>
                    </tr>
                      <tr>
                     <td><input type="checkbox"><span> Mosambi/orange</span></td>
                      <td>1 medium/40cal</td>
                      <td><input type="text"> Nos. </td>
                      <td><input type="text" class="uneditable-input" placeholder="aa"> cal.</td>
                    </tr>
                      <tr>
                     <td><input type="checkbox"><span> Papaya</span></td>
                      <td>1 piece/80cal</td>
                      <td><input type="text"> Nos. </td>
                      <td><input type="text" class="uneditable-input" placeholder="aa"> cal.</td>
                    </tr>
                      <tr>
                     <td><input type="checkbox"><span> Pineapple</span></td>
                      <td>1 piece/50cal</td>
                      <td><input type="text"> Nos. </td>
                      <td><input type="text" class="uneditable-input" placeholder="aa"> cal.</td>
                    </tr>
                      <tr>
                     <td><input type="checkbox"><span> Sapota</span></td>
                      <td>1 medium/80cal</td>
                      <td><input type="text"> Nos. </td>
                      <td><input type="text" class="uneditable-input" placeholder="aa"> cal.</td>
                    </tr>
                      <tr>
                     <td><input type="checkbox"><span> Watermelon/muskmelon</span></td>
                      <td>1 slice/15cal</td>
                      <td><input type="text"> Nos. </td>
                      <td><input type="text" class="uneditable-input" placeholder="aa"> cal.</td>
                    </tr>
                      <tr>
                     <td><input type="checkbox"><span> Custard apple</span></td>
                      <td>1 medium/130cal</td>
                      <td><input type="text"> Nos. </td>
                      <td><input type="text" class="uneditable-input" placeholder="aa"> cal.</td>
                    </tr>



                  </tbody>

html code for right table:

        <table class="table rt-table rt-table-border" id="tbl2">
        <thead style="background-color: #dff0d8">
            <tr>

              <th style="line-height: 11px !important;">Name</th>
              <th style="line-height: 11px !important;">Portion</th>
              <th style="line-height: 11px !important;">Calorie</th>
            </tr>
        </thead>
        <tbody id="">


        </tbody>
    </table>

and the script

     $(window).load(function(){
  $("#tbl1 input:checkbox.chkclass").click(function(){
//var fs=$(this).parent('div')
//  var span = $('span [for="'+this.id+'"]',fs)

    if ($(this).is(":checked"))

    {
     $(this).closest("tr").clone().appendTo("#tbl2");

/*
 $('#tbl2').append('<tr data-for="'
                        +span.text()
                        +'"><td>'
                        +span.text()
                        +'</td><td>'
                        +$(this).attr('value')
                        +'</td></tr>');

*/


    }
    else
    {
      var index = $(this).closest("tr").attr("data-index");
      var findRow = $("#tbl2 tr[data-index='" + index + "']");
      findRow.remove();
    }
  });
});

The commented code gives me undefined and un-commented code give the whole row.

I know the question is long, needed to give a descriptive idea.

valar morghulis

I have created a jsfiddle for you hope it works for you. click here

you can use the following code:

$("#tbl1 input:checkbox.chkclass").click(function(){
   var html = '';
   if ($(this).is(":checked")) {
     $(this).next().clone().appendTo("#tbl2");
     $(this).parent().next().next().clone().appendTo("#tbl2");
     $(this).parent().next().next().next().clone().appendTo("#tbl2");
   } else {
     var index = $(this).closest("tr").attr("data-index");
     var findRow = $("#tbl2 tr[data-index='" + index + "']");
     findRow.remove();
   }
});

Note: There are other alternates too.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

AngularJS: Copy the value of one input box to another input box only if checkbox is checked

From Dev

How to append table row to one table to another table using checkbox checked in javascript

From Dev

How to add a value from one table to a particular row in another table?

From Dev

Add row when selected from one table to another using php

From Dev

Add row when selected from one table to another using php

From Dev

jquery: hide input box if checkbox gets checked

From Dev

Copying Checked rows from one table to another table

From Dev

Show or hide table row if checkbox is checked

From Dev

how to add value from one combo box to another combo box

From Dev

if one checkbox is checked, how to uncheck another?

From Dev

if one checkbox is checked, how to uncheck another?

From Dev

add selected row from one gridview to another

From Dev

Copy a row from one table to another

From Dev

Jquery - copy a row from one table to another

From Dev

Move table columns from one row into another

From Dev

Add a table row from JSON input

From Dev

Input element from one list to add to another

From Dev

Checkbox - Fire one function when checked and fire another when not checked

From Dev

with an input checkbox inside a table row, can i add click event to row and still be able to click input

From Dev

how to set checkbox checked based on value in one array and add value to another array if checked otherwise remove using angular js?

From Dev

Copy row from one table to other without copying the checkbox

From Dev

MySQL add data from one table to another

From Dev

Joining row from one table with the sum value from another table

From Dev

Copy row from one table to other in the editable text box

From Dev

When I have Check Box checked how to delete row from Table View?

From Dev

Insert many rows from a table into one unique row in another table

From Dev

SQL query to select row from one table which is not in another table

From Dev

Delete from one table unless row is referenced in another table

From Dev

Getting all <input> data of a row that has a checked checkbox

Related Related

  1. 1

    AngularJS: Copy the value of one input box to another input box only if checkbox is checked

  2. 2

    How to append table row to one table to another table using checkbox checked in javascript

  3. 3

    How to add a value from one table to a particular row in another table?

  4. 4

    Add row when selected from one table to another using php

  5. 5

    Add row when selected from one table to another using php

  6. 6

    jquery: hide input box if checkbox gets checked

  7. 7

    Copying Checked rows from one table to another table

  8. 8

    Show or hide table row if checkbox is checked

  9. 9

    how to add value from one combo box to another combo box

  10. 10

    if one checkbox is checked, how to uncheck another?

  11. 11

    if one checkbox is checked, how to uncheck another?

  12. 12

    add selected row from one gridview to another

  13. 13

    Copy a row from one table to another

  14. 14

    Jquery - copy a row from one table to another

  15. 15

    Move table columns from one row into another

  16. 16

    Add a table row from JSON input

  17. 17

    Input element from one list to add to another

  18. 18

    Checkbox - Fire one function when checked and fire another when not checked

  19. 19

    with an input checkbox inside a table row, can i add click event to row and still be able to click input

  20. 20

    how to set checkbox checked based on value in one array and add value to another array if checked otherwise remove using angular js?

  21. 21

    Copy row from one table to other without copying the checkbox

  22. 22

    MySQL add data from one table to another

  23. 23

    Joining row from one table with the sum value from another table

  24. 24

    Copy row from one table to other in the editable text box

  25. 25

    When I have Check Box checked how to delete row from Table View?

  26. 26

    Insert many rows from a table into one unique row in another table

  27. 27

    SQL query to select row from one table which is not in another table

  28. 28

    Delete from one table unless row is referenced in another table

  29. 29

    Getting all <input> data of a row that has a checked checkbox

HotTag

Archive