1

I have a table. onchange of select variable, it generates rows. what i want to do is that, the data which i wrote in the first row, should be copied in the generated rows. This is my Table and the java script.

<form>
    <table border = "1" id = "engagements">
        <tr>
            <th><input type="checkbox" onclick="checkAll(this)"></th>
            <th>Organization</th>
            <th>Project</th>
            <th>Product</th>
            <th>Activity</th>
        </tr>
        <tr>
            <td><input type="checkbox" onclick="checkAll(this)"></td>
            <td><input type = "text"/></td>
            <td><input type = "text"/></td>
            <td><input type = "text"/></td>
            <td><input type = "text"/></td>
            <!--
            <td><input type = "text"/></td>
            <td><input type = "text"/></td>
            <td><input type = "text"/></td>
            <td><input type = "text"/></td>
            <td><input type = "text"/></td>
            -->
        </tr>
    </table>

    <select name = "mode" id = "mode" onchange="addrow('engagements')">
        <option value="">Add More Rows with Same Data as Above</option>
        <option value="1">1 More</option>
        <option value="2">2 More</option>
        <option value="3">3 More</option>
        <option value="4">4 More</option>
        <option value="5">5 More</option>
     </select>

</form>


<script language="javascript">
    function addrow(tableID)
    {
        var table = document.getElementById(tableID);
        var rowCount = table.rows.length;
        var row = table.insertRow(rowCount);
        var colCount = table.rows[0].cells.length;
        var e = document.getElementById("mode");
        var strUser = e.options[e.selectedIndex].value;

        for (var j = 0; j < strUser; j++)
        {
            for (var i = 0; i < colCount; i++)
            {
                var newcell = row.insertCell(i);
                newcell.innerHTML = table.rows[1].cells[i].innerHTML;
            }
            var row = table.insertRow(rowCount);
        }
    }
</script>
3
  • Can you setup a fiddle for this? Commented Jun 21, 2013 at 12:00
  • you forget to close mode dropdown Commented Jun 21, 2013 at 12:03
  • i corrected it. no change in output Commented Jun 21, 2013 at 12:06

4 Answers 4

2

If you are already using jQuery , You can do this -

$("#mode").on('change', function () {
    var rows = parseInt(this.value);
    var lastRow;
    for (var i = 0; i < rows; i++) {
        lastRow = $('#engagements tr').last().clone();
        $('#engagements tr').last().after(lastRow);
    }
});

Demo ----> http://jsfiddle.net/jW6eL/

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

Comments

1
$('#step_table').append($('#step_table tr:last td:last').html());

Comments

0

Try this

function addrow(tableID)
{
  var $table = $('#' + tableID);
  var noOfRows = parseInt($('#mode').val());
  var $clonedRow = $table.find('tr:last');
  for(var i = 0; i < 5; i++) {
    $clonedRow.clone().appendTo($table);
  }
}

Comments

0

Replace your function with this function (jQuery solution):

function addrow(tableID)
{
    var table=$('#'+tableID);
    var val1=$('#mode').val();

    for(i = 0; i < parseInt(val1); i++)
    {
        table.find('tr:last').after('<tr>'+table.find('tr:last').html()+'</tr>');
    }
}

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.