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 via email to