Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
addUnique
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Iconico  
View profile  
 More options May 1 2008, 2:30 pm
From: Iconico <icon...@iconico.com>
Date: Thu, 1 May 2008 11:30:23 -0700 (PDT)
Local: Thurs, May 1 2008 2:30 pm
Subject: 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');


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dave Methvin  
View profile  
 More options May 1 2008, 7:42 pm
From: Dave Methvin <dave.meth...@gmail.com>
Date: Thu, 1 May 2008 16:42:24 -0700 (PDT)
Local: Thurs, May 1 2008 7:42 pm
Subject: Re: addUnique

> 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.

That actually sounds like a bug. What version of jQuery are you using?
AFAICT, jQuery().add() is supposed to use jQuery.merge to avoid
duplicates. Can you create a test case and post a bug report?

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Iconico  
View profile  
 More options May 6 2008, 12:50 pm
From: Iconico <icon...@iconico.com>
Date: Tue, 6 May 2008 09:50:36 -0700 (PDT)
Local: Tues, May 6 2008 12:50 pm
Subject: Re: addUnique
On May 1, 7:42 pm, Dave Methvin <dave.meth...@gmail.com> 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/f08ee33...

If you still don't follow I can mock up a test for you, just let me
know.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ariel Flesler  
View profile  
 More options May 6 2008, 8:22 pm
From: Ariel Flesler <afles...@gmail.com>
Date: Tue, 6 May 2008 17:22:19 -0700 (PDT)
Local: Tues, May 6 2008 8:22 pm
Subject: Re: addUnique
This is actually a bug, I opened a ticket and added a diff, will be
fixed soon.

http://dev.jquery.com/ticket/2811

--
Ariel Flesler
http://flesler.blogspot.com

On 6 mayo, 13:50, Iconico <icon...@iconico.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google