2

MY JAVASCRIPT CODE

var a1=0;
var count=1;
function addRow(data)
    {
        var table = document.getElementById('student');
        var rowCount = table.rows.length;
        var col = table.rows[2].cells.length;
        var del = (data.id);
        var row = table.insertRow(rowCount);
        for(i=0;i<col;i++)
        {
            var newcell = row.insertCell(i);
            newcell.innerHTML = table.rows[2].cells[i].innerHTML; 
        }       
    }

function addst(data)
  {
    var table = document.getElementById('student');
    var rowCount = table.rows.length;
    var row =  table.insertRow(rowCount);
    row.innerHTML = '&nbsp;';
    for(i=0;i<3;i++)
        {   
            var row = table.insertRow(rowCount+i+1);
            row.innerHTML = table.rows[i].innerHTML;    
            if(i==0)
            {
                row.id="st"+a1+"";
            }
        }       
    count++;
}

function delRow(data) 
{
    var table = document.getElementById('student');
    var rowCount = table.rows.length;
        var p=data.parentNode.parentNode;
        p.parentNode.removeChild(p);
}

function delst(data)
{
    var table = document.getElementById('student');
    var rowCount = table.rows.length;
    var p = document.getElementById(eq01);
    table.removeChild(p);
}

MY HTML CODE

<fieldset>
<legend align="center">Student Information</legend> 
<br>
<table id="student">
    <tr id="s1">        
        <th><select name="" width="250px"> 
        <option value="" selected align="center">student Type</option>
        <option value="">male</option>
        <option value="">female</option>
        </select></th>
        <th><input type="button" value="+" onclick="addst(this)"/>
        <th><input type="button" value="-" onclick="delst(this)"/>
    </tr>

    <tr id="label">
        <th align="left">class info</th>
        <th align="left">Math</th>
        <th align="left">Phy</th>
        <th align="left">Chem</th>
        <th align="left">Total</th>
        <th align="left">Average</th>   
    </tr>
    <tr id="c1">
        <td>
            <select> 
                <option value="" selected>Select your class</option>
                <option>8</option>
                <option>9</option>
                <option>10</option>
                <option>11</option>
            </select>
        </td>

        <td><input type="text" id="val02" size="16" name="math" placeholder="Math marks"/></td>
        <td><input type="text" id="val03" size="16" name="phy" placeholder="Phy marks"/></td>
        <td><input type="text" id="val04" size="16" name="chem" placeholder="Chem marks"/></td>
        <td><input type="text" id="val06" size="16" name="total" placeholder="Total marks"/></td>
        <td><input type="text" id="val07" size="16" name="avg" placeholder="Average marks"/></td>
        <th><input type="button" value="+"  id="r1" onclick="addRow(this)" /></th>          
        <th><input type="button" value="-"  id="r1" onclick="delRow(this)" /></th>
    </tr>
</TABLE>
</fieldset>

The 'addst' and 'addRow' functions are working but have no idea about how delete functions should be written....can u guys please help me out

'delst' should delete the whole respective student information while 'delRow' should delete the each individual row

4
  • What is your java script code? Commented Dec 14, 2015 at 7:29
  • posted just check it.....no idea what to use for delst....can u help me out? Commented Dec 14, 2015 at 7:30
  • Post your full code. So many variables are showing undefined. Look at you console Commented Dec 14, 2015 at 7:33
  • hey sanjay check now i have declared the variables...it should work now!! Commented Dec 14, 2015 at 7:37

3 Answers 3

1

You have written a function that name DeleteRow but you are calling delRow(this) in the click event. because of that you are getting error.

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

1 Comment

i have changed it now delRow is working but delst is not :p
0

Try like this

function delst(data)
{ 
    var table = document.getElementById('student');
    var rowCount = table.rows.length;
    var p=data.parentNode.parentNode;
    p.parentNode.removeChild(p);
}

1 Comment

I tried it before itself but it deletes only the present row but i want to delete the whole part of student information!!
0

Please bind each data in separate table and then try to delete that table under row. it will easy to delete parent child elements. example:

<table>
<tr>
<td>
<table><tr><!--your row data--><td></td></tr></table>
</td>
</tr>
</table>

2 Comments

Dude i tried it, 'delst' is working but 'addRow' is not working all new rows are adding to the first table itself as I couldn't get a reference for the next created table. Can u help me out?
Hi Nani, Please go through this link. may be this will help you :) viralpatel.net/blogs/…

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.