Disclaimer: I am fully aware that the id
attribute is for unique
IDs. In my case, when the ajax request takes longer than usual it can glitch, causing two same chat messages. I am also aware that there are similar questions out there like this, but I have been unable to find one that solves my issue.
This is what I want to do:
- Are there any duplicate IDs inside the div
chat_log
? - What are they?
- Delete all the duplicates
- Making sure that the original one is still there.
I've tried using the following code:
$('[id]').each(function () {
var ids = $('[id=' + this.id + ']');
if (ids.length > 1 && ids[0] == this) {
$(ids[1]).remove();
}
});
But I'm not sure how I can adapt that method to my situation, nor am I sure if it would be possible.
Disclaimer: I am fully aware that the id
attribute is for unique
IDs. In my case, when the ajax request takes longer than usual it can glitch, causing two same chat messages. I am also aware that there are similar questions out there like this, but I have been unable to find one that solves my issue.
This is what I want to do:
- Are there any duplicate IDs inside the div
chat_log
? - What are they?
- Delete all the duplicates
- Making sure that the original one is still there.
I've tried using the following code:
$('[id]').each(function () {
var ids = $('[id=' + this.id + ']');
if (ids.length > 1 && ids[0] == this) {
$(ids[1]).remove();
}
});
But I'm not sure how I can adapt that method to my situation, nor am I sure if it would be possible.
Share Improve this question edited Jul 10, 2014 at 11:15 Mehraban 3,3345 gold badges39 silver badges63 bronze badges asked Sep 20, 2012 at 19:03 Jordan RichardsJordan Richards 5423 silver badges18 bronze badges 6- You say you tried that code... did it work? Are you asking for help changing that code for your situation or are you saying that is what you tried and it didn't work so you need a different solution? – MrOBrian Commented Sep 20, 2012 at 19:07
-
That code will work (potentially add the
#chat_log
selector). However, appending duplicate-id content should never be risked depending on ajax loading times. Show us your app logic, there must be a flaw in it. – Bergi Commented Sep 20, 2012 at 19:09 - @MrOBrian I should have been more clear on that. But didn't you read "I've tried using the following code:"? Anyway, I am open to a different solution, but at the same time, if it's easier for you, fixing the current code. – Jordan Richards Commented Sep 20, 2012 at 19:10
- @JordanRichards: Yes, we read that line. But you didn't gave us any information why it would not have worked for you; what feature needs an adaption? – Bergi Commented Sep 20, 2012 at 19:12
- Yes, I did read "I've tried using the following code:" which is why I said "You say you tried that code". What you did not say, however, is whether that code worked at all, errored, worked in different situation but not this one, etc. – MrOBrian Commented Sep 20, 2012 at 19:12
2 Answers
Reset to default 2How can you ensure that something is unique? Let's say you have a bunch of vegetables (cucumbers, turnips, pizzas etc.) You want to ensure colour uniqueness, making sure that any colour only appears once. How'd you do it?
What I'd do is make a list. I'd go through every vegetable, and inspect its colour. If the colour is already on the list, we'll remove that vegetable from the bunch. Otherwise, we leave it as-is and add its colour to our list.
Once that logic is understood, all we need is to convert it to code! What a fantastically trivial thing to do (on paper, of course.)
//assumes $elem is the element you're deleting duplicates in
//create the ids list we'll check against
var ids = {};
//go over each element
var children = $elem.children();
for ( var i = 0, len = children.length; i < len; i++ ) {
var id = children[ i ].id;
//was this id previously seen?
if ( ids.hasOwnProperty(id) ) {
$( children[i] ).remove();
}
//a brand new id was discovered!
else {
ids[ id ] = true;
}
}
//done!
This is the very simple, plain logic version. You can make much fancier ways with some weird sizzle selectors, but this should get you started.
Demo (without jquery): http://tinkerbin./qGJpPsAQ
Your code should work but it only removes the second element that has the same ID, try this:
$('[id]').each(function() {
var $ids = $('[id=' + this.id + ']');
if ($ids.length > 1) {
$ids.not(':first').remove();
}
});
http://jsfiddle/3WUwZ/