Thursday, 23 August 2012

Copy Table Row Using DOM Object

// Call this function onClick event in javascript for copying TR
function addRow(tableID)
{
    dml=document.forms['add_products'];// Form Name
               
    // get the number of elements from the document
    len = dml.elements.length;
     for( i=0 ; i<len ; i++)
     {
        //check the textbox with the elements name
            if (dml.elements[i].name=='rest_prod_price[]')
            {
              // if exists do the validation and set the focus to the textbox
                if (dml.elements[i].value=="")
                {
                    alert("Invalid empty data");
                    dml.elements[i].focus();
                    return false;           
                }
            }
     }
   
     var table = document.getElementById(tableID);
    var rowCount = table.rows.length;
    var row = table.insertRow(rowCount);
    var colCount = table.rows[0].cells.length;
   
    for(var i=0; i<colCount; i++)
    {
        var newcell = row.insertCell(i);
        newcell.innerHTML = table.rows[0].cells[i].innerHTML;
    }
}

 // Call this function onClick event in javascript for delete TR
function deleteRow(tableID)
{   
     var table = document.getElementById(tableID);
    var rowCount = table.rows.length;
    if(rowCount > 1)
    {table.deleteRow(rowCount - 1);}
    else
    {alert('No More Control To Delete.');}
   
}

No comments:

Post a Comment