0

I have developed two tables "TABLE1" and "TABLE2". I want if i enter subject name in Table 1 then it will automatically copy in Table 2 and if click on "Add new+" button of Table1 to add new row then this change will happen on Table2.

$("#insert66").click(function () {
     $("#table66").each(function () {
         var tds = '<tr>';
         jQuery.each($('tr:last td', this), function () {
             tds += '<td>' + $(this).html() + '</td>';
         });
         tds += '</tr>';
         if ($('tbody', this).length > 0) {
             $('tbody', this).append(tds);
         } else {
             $(this).append(tds);
         }
     });
});
Table1:-
<table id="table66" class="table table-bordered table-hover">
								 <input type="button" class="btn green" value="Add New+" id="insert66"></input>
    <thead> 
        
        <th>Subject Name</th>
		
        
    </thead>
    <tbody>
        <tr>
            
            <td>
                <input type="text" class="form-control subName" name="subName">
            </td> 
			
 
        </tr>
    </tbody>
</table>

Table2:- 

<table id="tablecourse" class="table table-striped table-hover">
								 
    <thead>
        <tr>
        <th>Subject Name</th>
		<th>Campus</th>
		
        </tr>
    </thead>
    <tbody>
        <tr>
            
            <td>
                <input type="text" class="form-control subName" name="subName"> 
            </td>
			<td>
                <select id="multipleSelectExample4" style="width:100%;" data-placeholder="Select an option">
                          <option value="1">Option 1</option>
                          <option value="2">Option 2</option>
                          <option value="3">Option 3</option>
                          <option value="4">Option 4</option>
                          <option value="5">Option 5</option>
                      </select>
            </td>
			
 
        </tr>
		
    </tbody>
</table>

I have found one logic but bit clueless how to use this

var counterClass = 0
$("#table66 input").change(function(){

 if($(this).hasClass("completed")) {
// update
 $("." + $(this).attr("refClass")).html($(this).val())
 } else {
 // append/insert

 $(this).addClass("completed").attr("refClass", "refClass" + counterClass)
 counterClass += 1

 $().append()

 }
 })
1
  • The code you provided is not working, Also its not well formatted. Can you update your code to workable demo. Commented Feb 3, 2017 at 8:05

2 Answers 2

2

Try with index() match .Apply the Append function in both table. $('table').each() .First table input value reflect with second table input value with same index match

update

3 table function. Dont forget to new table id in this function see below snippet

$(document).ready(function(){
$("#insert66").click(function() {
  $(".table").each(function() {
    var tds = '<tr>';
    jQuery.each($('tr:last td', this), function() {
      tds += '<td>' + $(this).html() + '</td>';
    });
    tds += '</tr>';
    if ($('tbody', this).length > 0) {
      $('tbody', this).append(tds);
    } else {
      $(this).append(tds);
    }
  });
 
});
   $('table').on('input','.subName',function(){
         var index =$(this).closest('table').find('input').index(this);
         $('#tablecourse').find('input[type=text]').eq(index).val($(this).val())
//for second table
         $('#table3').find('input[type=text]').eq(index).val($(this).val())
//for 3rd table
    })
  })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Table1:-
<table id="table66" class="table table-bordered table-hover">
  <input type="button" class="btn green" value="Add New+" id="insert66"></input>
  <thead>

    <th>Subject Name</th>


  </thead>
  <tbody>
    <tr>

      <td>
        <input type="text" class="form-control subName" name="subName">
      </td>


    </tr>
  </tbody>
</table>

Table2:-

<table id="tablecourse" class="table table-striped table-hover">

  <thead>
    <tr>
      <th>Subject Name</th>
      <th>Campus</th>

    </tr>
  </thead>
  <tbody>
    <tr>

      <td>
        <input type="text" class="form-control subName" name="subName">
      </td>
      <td>
        <select id="multipleSelectExample4" style="width:100%;" data-placeholder="Select an option">
          <option value="1">Option 1</option>
          <option value="2">Option 2</option>
          <option value="3">Option 3</option>
          <option value="4">Option 4</option>
          <option value="5">Option 5</option>
        </select>
      </td>


    </tr>

  </tbody>
</table>
Table3:-

<table id="table3" class="table table-striped table-hover">

  <thead>
    <tr>
      <th>Subject Name</th>
      <th>Campus</th>

    </tr>
  </thead>
  <tbody>
    <tr>

      <td>
        <input type="text" class="form-control subName" name="subName">
      </td>
      <td>
        <select id="multipleSelectExample4" style="width:100%;" data-placeholder="Select an option">
          <option value="1">Option 1</option>
          <option value="2">Option 2</option>
          <option value="3">Option 3</option>
          <option value="4">Option 4</option>
          <option value="5">Option 5</option>
        </select>
      </td>


    </tr>

  </tbody>
</table>

Sign up to request clarification or add additional context in comments.

4 Comments

is it possible that we can do same this on 3rd table. I mean whatever action hapen on 1st table will be on 3rd table as well with table2?
@ArpitGupta yes create the 3rd table in your html .Its will post the value to both 2nd and 3rd table
i tried with common table id for 2nd and 3rd table but not working for me . could you please update above jsfiddle . it will be a great help
@ArpitGupta yes sure.. See my updated answer.Its ok..id's are unique's so try with different name of id
0

Try this one. if you need any help just ask :)

I've add a invisible template row. This will be added when ever you click the Add-Button. And a function to copy the text from one input field to the other.

function addRow() {
    $('#table1').append('<tr>'+$('#table1 tr.template').html()+'</tr>');
    $('#table2').append('<tr>'+$('#table2 tr.template').html()+'</tr>');
}

function updateTabel(source, id){
    var text = $(source).val();
    var row = $(source).closest('tr').index();
    $($('#'+id+' tr')[row]).find('input.subName').val(text);
}
    .template {
        display: none;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="button" value="Add New" onclick="addRow()" /><br>
<br>
Table1:-
<table id="table1">
    <tr>
        <th>Subject Name</th>
    </tr>
    <tr class="template">
        <td>
            <input type="text" class="subName" onKeyUp="updateTabel(this, 'table2')">
        </td> 
    </tr>
</table>

Table2:- 

<table id="table2">
    <tr>
        <th>Subject Name</th>
        <th>Campus</th>
    </tr>
    <tr class="template">
        <td>
            <input type="text" class="form-control subName" onKeyUp="updateTabel(this, 'table1')"> 
        </td>
        <td>
            <select id="multipleSelectExample4" style="width:100%;" data-placeholder="Select an option">
                <option value="1">Option 1</option>
                <option value="2">Option 2</option>
                <option value="3">Option 3</option>
                <option value="4">Option 4</option>
                <option value="5">Option 5</option>
            </select>
        </td>
    </tr>
</table>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.