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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jul 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JS to remove TR attributes

    Hi Guys,

    I'm new at JS.
    I need to remove TR elements from parent table but the problem is there are no table ID/Name

    Is it possible to perform it?
    Please see attach - i need remove red marked block... what scrip i have to use if i will put it to the green block?
    Thank you.
    Attached Thumbnails Attached Thumbnails JS to remove TR attributes-removetr.png  

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Despite the similar sounding names, Java is not the same as Javascript.
    Moving from Java forum to Javascript forum.
    DOM manipulation should include removal of child nodes, the JS people can give you more help with that.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,151
    Thanks
    80
    Thanked 4,557 Times in 4,521 Posts
    Assuming that, indeed, there is only one
    Code:
        <td id="TopCell" ... >
    on that page (as indeed there should be, since id's should be unique) and assuming that you want to remove the second and third <tr>s from that enclosed <table>, then *removing* the rows is easy.

    *MOVING* the rows to the green-marked <div> is not so easy.

    Does that <div> already contain a <table>???

    If not, we would have to create a <table> inside the <div> to put the <tr> rows into.

    It would help if you would show that <div>'s contents, expanded out. At least one level.

    ***********

    But it is clear that this page is being generated by ASP.NET. Wouldn't it be easier to change the ASP.NET code to do this, rather than trying to do it with JavaScript??
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,372
    Thanks
    32
    Thanked 286 Times in 280 Posts
    Theoretically, the following code should do what you want. Unfortunately, I can't confirm that since it hasn't been tested since you didn't supply your source code nor is your request 100% clear due to your use of incorrect grammar and the issue noted by Old Pedant.

    Additionally, the code would have been simpler had I been able to see the entire ID of the div element. It's only partially visible in the reference image.

    Code:
    var TopCell = document.getElementById("TopCell");
    if (typeof TopCell.firstElementChild !== "undefined" && typeof TopCell.lastElementChild !== "undefined") {
        // W3C DOM3 Core + W3C Element Traversal Method
        var table_element = TopCell.firstElementChild.cloneNode(true);
        var tbody_element = table_element.firstElementChild;
        tbody_element.removeChild(tbody_element.firstElementChild);
        div_element = document.getElementById("MSOZoneCell_WebPartWPQ5").firstElementChild.firstElementChild.firstElementChild.firstElementChild.firstElementChild;
        div_element.appendChild(table_element);
    }
    else if (typeof TopCell.getElementsByTagNameNS === "function") {
        // W3C DOM3 Core Method
        // Namespace‐Aware
        var XHTML_NS = "http://www.w3.org/1999/xhtml";
        var table_element = TopCell.getElementsByTagNameNS(XHTML_NS, "table").item(0).cloneNode(true);
        var tbody_element = table_element.getElementsByTagNameNS(XHTML_NS, "tbody").item(0);
        tbody_element.removeChild(tbody_element.getElementsByTagNameNS(XHTML_NS, "tr").item(0));
        div_element = document.getElementById("MSOZoneCell_WebPartWPQ5").getElementsByTagNameNS(XHTML_NS, "div").item(0);
        div_element.appendChild(table_element);
    }
    else {
        // W3C DOM3 Core Method
        // Not Namespace‐Aware
        var table_element = TopCell.getElementsByTagName("table").item(0).cloneNode(true);
        var tbody_element = table_element.getElementsByTagName("tbody").item(0);
        tbody_element.removeChild(tbody_element.getElementsByTagName("tr").item(0));
        div_element = document.getElementById("MSOZoneCell_WebPartWPQ5").getElementsByTagName("div").item(0);
        div_element.appendChild(table_element);
    }
    For every complex problem, there is an answer that is clear, simple, and wrong.


  •  

    Posting Permissions

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