C_K
C_K

Reputation: 1243

Selector :first Error with For Loop

A user specifies a numerical value; then a for loop runs it that many times. I have a series of these types of set ups, and they all work, except this one. I believe it's because of the :first selector I've used.

    jQuery.fn.removeX = function() {
        for (var i = 0; i < xTimes; i++) {
            $("#Tell").append(' removed!');
            $("#List_" + C_user + " img:first").relocate();
        }
    }

I've tried variations, such as :nth-child(0), even .slice(), but so far nothing works. However, the .relocate function, it's designed only to handle one element at a time. I didn't think should make a difference, but if I switch it out for something like .appendTo, it works fine.

The .relocate script:

    jQuery.fn.relocate = function(){
        getSRC2 = $(this).find('img').attr('src');
        $(this).prependTo(".List_" + Owner).removeAttr('style');
        $(this).remove();
    }

It's nothing special, so I'm not sure how it breaks the other one.

Any tips/pointers would be helpful.

SOLUTION

Removed the .find(), worked, thanks! I was trying to reuse the script, but the objects it was originally designed for are wrapped, hence the .find().

Upvotes: 0

Views: 51

Answers (1)

user1106925
user1106925

Reputation:

Here, you're selecting an img...

$("#List_" + C_user + " img:first").relocate();

Then in .relocate(), you're doing a .find() on the img...

getSRC2 = $(this).find('img').attr('src');

An img can't be nested inside an img.


Either get rid of the .find('img') since you already have it, or get rid of the img part of the selector...

$("#List_" + C_user).relocate();

Upvotes: 2

Related Questions