Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2007
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Dynamically remove TD using javascript

    Hi,

    How to remove TD dynamically using javascript. I've mentioned below my code.

    <table id="firstTbl" border="0" cellspacing="0" cellpadding="0" width="100%">
    <tr id="firstTr">
    <td id="firstTd"> </td>
    <td id="secondTd"> </td>
    </tr>
    </table>

    I need to remove "firstTd" from the table..

    Can help me anybody.. ?


    -Thanks

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Code:
    ...
    var myTD=document.getElementById('firstTd');
    myTD.parentNode.removeChild(myTD);
    ...
    But, in fact, you don't need an id for the table elements, you may use the index of the cell in its row (or in the table)
    Code:
    ...
    var myTD=document.getElementById('firstTbl').getElementsByTagName('tr')[0].getElementsByTagName('td')[0];
    myTD.parentNode.removeChild(myTD);
    ...
    or
    Code:
    ...
    var myTD=document.getElementById('firstTbl').getElementsByTagName('td')[0];
    myTD.parentNode.removeChild(myTD);
    ...
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    New Coder
    Join Date
    Jul 2007
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Kor View Post
    Code:
    ...
    var myTD=document.getElementById('firstTd');
    myTD.parentNode.removeChild(myTD);
    ...
    But, in fact, you don't need an id for the table elements, you may use the index of the cell in its row (or in the table)
    Code:
    ...
    var myTD=document.getElementById('firstTbl').getElementsByTagName('tr')[0].getElementsByTagName('td')[0];
    myTD.parentNode.removeChild(myTD);
    ...
    or
    Code:
    ...
    var myTD=document.getElementById('firstTbl').getElementsByTagName('td')[0];
    myTD.parentNode.removeChild(myTD);
    ...


    Hi buddy,

    Thanks for your reply.. ur codes r working properly..
    i've tried with
    document.getElementById("firstTr").removeChild(document.getElementById("firstTd"));
    its also working...

    Thanks and regards,
    NM


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •