I have a very simple program. I want to add an object to a jquery set, but if that object already exists in the set, I want to remove it.
var images = $([]); //empty set
$(".ad input[type=checkbox]").click(function() {
var img = $(this).parent().children('img'); //my obj
if (images.find(img).length > 0) { //obj exists, remove it
images = images.not(img);
} else if (images.find(img).length == 0) { //obj not found, add it
images = images.add(img);
}
});
This crappy code (that I wrote myself) keeps defaulting to the second statement. I.e., it adds, but doesn't ever remove, even when the obj already exists in the set. That means there's probably something wrong with my find() call. Does anybody know what I'm doing wrong?
Also tried with failure:
var images = $([]);
$(".ad input[type=checkbox]").click(function() {
var img = $(this).parent().children('img');
if (images.has(img).length != 0) {
images = images.detach(img);
} else if (images.has(img).length == 0) {
images = images.add(img);
}
});
When logging the results, I found that
images.has(img).length
Always returns 0. Always. It should return 1 if it finds the img object inside the images set, no?