[jQuery] Re: addUnique
On May 1, 7:42 pm, Dave Methvin <[EMAIL PROTECTED]> wrote: > That actually sounds like a bug. What version ofjQueryare you using? > AFAICT,jQuery().add() is supposed to usejQuery.merge to avoid > duplicates. Can you create a test case and post a bug report? I'm using 1.2.3, the latest. I don't think it's a bug, it's more subtle than that. There's a thread here that details the issue pretty well: http://groups.google.com/group/jquery-en/browse_thread/thread/f08ee3387b7522b/46b61b9fe4e7d4b6 If you still don't follow I can mock up a test for you, just let me know.
[jQuery] addUnique
Just thought that this might help someone's pain. I have a group of objects and I wanted to add an additional object to it, simply by doing this: jQuery('#divMain img').add('#newImg'); The problem was that in my situation '#divMain img' sometimes already contains '#newImg', and when it does the add function would add a second copy of it, which messed things up further down the line. What I really needed was a way of uniquly adding an object to a selection. A bit of tinkering later and here's the function: jQuery.fn.addUnique = function(selector) { return this.filter(function (index) { return (jQuery(this).get(0) != jQuery(selector).get(0)) }).add(selector); }; You can just call it like this: jQuery('#divMain img').addUnique('#newImg');