-3
<td id="table_line_0">
<input id="btnAdd0" type="button" value="+" onClick="newLine()">
</td>
<td id="table_line_0">
<input id="btnAdd1" type="button" value="+" onClick="newLine()">
</td>

I need a javascript function to remove the two id (table_line_0)... tks!!

function removeID(id)
{

 ... I do not know what to do to find duplicate id ..

 line_table = document.getElementById(id);
 line_table.parentNode.parentNode.removeChild(line_table.parentNode);
}

I have two 'tr' with same id ... I want to remove the 'tr' with the same id..

Fabio
  • 64
  • 2
  • 9
  • 1
    Have you tried anything...? – tymeJV Feb 11 '15 at 21:17
  • 10
    Why do you have duplicate `id`s; given that you shouldn't (ever) have duplicate `id`s, what's happening to cause the problem? Solve *that* problem, not the symptoms. – David Thomas Feb 11 '15 at 21:17
  • You shouldn't have duplicate `id`s in the first place. It's not valid HTML. I'd suggest tackling the problem at the source rather than trying to fix it later. I guess you could keep using `getElementById` and then removing the element you get until there are none left and then add back either the first or the last as appropriate. But that's awfully hacky. – Matt Burland Feb 11 '15 at 21:19
  • 2
    Do you want to (a) remove the second element (b) remove both elements (c) just remove the `id` from the second element or (d) remove the `id` from both? – Paul Roub Feb 11 '15 at 21:21
  • If this is your source code, it will always be invalid, no matter what JS does with the code... – LcSalazar Feb 11 '15 at 21:21
  • I want to remove the two id!! I found this http://dev.enekoalonso.com/2011/05/06/finding-duplicate-ids-on-an-html-page/ but it not worked! – Fabio Feb 11 '15 at 21:26
  • 1
    `document.querySelectorAll('#table_line_0')` will select all the elements with the given `id`, just [iterate the collection through](http://jsfiddle.net/hrnq4vrp/), and do what you need. Though I agree with David Thomas, it's better not to create duplicated `id`s at the first place. – Teemu Feb 11 '15 at 21:30
  • 1
    Still not really clear. Do you want to remove the `id` attribute? Or remove one of the two `td` elements? The code you link to just looks for duplicates, it doesn't remove them. – Matt Burland Feb 11 '15 at 21:36
  • I've a doubt, that you're adding a clone of an existing row in `newLine` function. Just remove/change the `id`s in the clone before adding it to the DOM. After your edit, it looks like you'd want to remove the elements with duplicated `id`s rather than the `id`s themselves? – Teemu Feb 11 '15 at 21:45
  • I want to remove the entire line not only the ID... – Fabio Feb 11 '15 at 21:49
  • You mean you want to remove the `tr` element containing `td`s with duplicated `id`s? – Teemu Feb 11 '15 at 21:53
  • I have two 'tr' with same id ... I want to remove the 'tr' with the same id – Fabio Feb 11 '15 at 22:02
  • @Fabio In the post you seem to have two `td`s with the same `id`. Though you can use the code in my answer to remove the `tr`s as well. – Teemu Feb 11 '15 at 22:05

2 Answers2

1

If I've undertood your question correctly (accurated in commnets), you want to remove the whole tr containing tds with duplicate ids. This is what you can do:

function removeTr(id) {
    var ids = document.querySelectorAll(id), // Get a collection of cells with same ids
        len = ids.length,
        n;
    if (len < 2) {return;} // Quit, no duplicated ids
    for (n = 0; n < len; n++) { // Iterate through the collection
        if (ids[n]) { // Check, if the element exists
            ids[n].parentElement.removeChild(ids[n]); // If the wanted id is found, remove the parent
        }
    }
}

A live demo at jsFiddle.

Then just call removeTr('#table_line_0'); using the wanted id as an argument.

Teemu
  • 22,918
  • 7
  • 53
  • 106
  • @Fabio Sorry, I forgot to add a fiddle link. I've included it to my answer now. If you can't get this to work with your code, maybe it's a place for a new question. In the current question there's not information enough to solve your problem. A further edition would change the question totally (which would make other answers looking weird), hence rather a new question. – Teemu Feb 11 '15 at 22:16
  • change 'ids[n].parentElement.removeChild(ids[n]);' to 'ids[n].parentNode.parentNode.removeChild(ids[n].parentNode);' perfect!!! nice! tks @Teemu – Fabio Feb 11 '15 at 22:32
  • 1
    @Fabio I'm glad you could solve your problem. For the future, please take time to add all information (and correct) to your question before posting it. And please stay online for at least ten minutes after posting. That way we can avoid to ask further information. And if some questions appear in the comments, you can quickly answer them, and we can avoid over-length comment threads, and you'll get your answer much faster, – Teemu Feb 11 '15 at 22:38
-1

Well having duplicate IDs is kind of stupid in the first place. But if you somehow did, perhaps loading multiple ajax sources that have the same IDS. Then you can remove the IDs like this:

HTML

<div id='id1'>ID1</div>
<div id='id1'>ID1</div>

JavaScript (jQuery)

do {
    $("#id1").removeAttr("id");
} while ( $("#id1").size() > 0 );

Edit

JavaScript (no jQuery)

do {
    document.getElementById("id1").removeAttribute("id");
} while (document.getElementById("id1") != null);

Hope this helps

Dustin Poissant
  • 3,201
  • 1
  • 20
  • 32