2

I know this has been asked and answered a couple times already, but I'm still confused about how to reference the current object when iterating over a jQuery array. For example, the following code gives me the error TypeError: genH3Array[i].next is not a function. What is the right way to reference the current array object?

var genH3Array = $('#Generation_II').parent();
    genH3Array.push($('#Generation_III').parent());;
    genH3Array.push($('#Generation_IV').parent())

$.each(genH3Array, function(i, value)
        {
            if(genH3Array[i].next().attr("align") == "center")
            {                   genH3Array[i].next().next().next().insertBefore(heading.next())
            }
            genH3Array[i].next().next().insertBefore(heading.next())
            genH3Array[i].next().insertBefore(heading.next())
        })

EDIT: Thanks for all your help, everyone. I know this was probably a cinch for most of you, but it was a major headache for me. The corrected code is below:

var genH3Array = $('#Generation_II,#Generation_III,#Generation_IV').parent();

        $.each(genH3Array, function(i, value)
        {
            console.log($(this).next());

            if($(this).next().attr("align") == "center")
            {
                $(this).next().next().next().insertBefore(pokemonHeader.next())
            }
            $(this).next().next().insertBefore(pokemonHeader.next())
            $(this).next().insertBefore(pokemonHeader.next())
            $(this).insertBefore(pokemonHeader.next())
        })
5
  • With the value property, presumably. That's what it's there for, no? Commented Jan 7, 2011 at 2:45
  • Note that genH3Array is not an array, but a jQuery object. That might be why you're having trouble with the [] and subscript. Check out api.jquery.com/jQuery.each. Commented Jan 7, 2011 at 2:46
  • I thought of that as well, but I get the same error: "TypeError: value.next is not a function" Commented Jan 7, 2011 at 2:47
  • Because value is a DOM object. Form a jQuery object from it like $(value).next()... Commented Jan 7, 2011 at 2:52
  • Instead of .insertBefore(pokemonHeader.next()), it would seem that you could do .insertAfter( pokemonHeader ), since before the next item after pokemonHeader would place it after pokemonHeader. Just a thought. ;o) Commented Jan 7, 2011 at 17:59

4 Answers 4

3

This part:

var genH3Array = $('#Generation_II').parent();
    genH3Array.push($('#Generation_III').parent());
    genH3Array.push($('#Generation_IV').parent());

...isn't really the way to use .push() against a jQuery object. When you .push() a value in, it should be a DOM element. Not a jQuery object.

You could simplify that entire bit like this:

var genH3Array = $('#Generation_II,#Generation_III,#Generation_IV').parent();

Now you'll have the .parent() of all three in the object.

Not entirely sure what the each is supposed to do, but it seems like you're trying to take the next three elements of each one, and insert them after some heading element.

$.each(genH3Array, function(i, value) {
        if($(this).next().attr("align") == "center") {                    
            heading.after( $(this).nextUntil('sometarget:last') );
        }
        heading.after( $(this).nextUntil('sometarget') );
    });

I really don't know if this is what you want. It's a little hard to tell.

Sign up to request clarification or add additional context in comments.

5 Comments

What you say is correct, but doesn't address his problem. Using square brackets on a jQuery array, however created, will always cause the error he is seeing.
@Malvolio: Technically his use of the square brackets is correct for two of the items since he was pushing full jQuery objects into the jQuery object. Since this is not the right approach, I was trying to make the code into something more conventional. But you're right, that the first one is a DOM element.
I'm getting this TypeError after using the simplified one-line declaration you supplied for genH3Array: TypeError: "#Generation_II,#Generation_III,#Generation_IV".parent is not a function. I don't know if I mentioned this before, but each object's parent is not the same as the parents of the other generation elements.
@DeltaFox: Shouldn't matter if the parent elements are the same. But based on the error in your comment, it looks like you're calling parent() against a string. Make sure it is wrapped in $() as in $('#Generation_II,#Generation_III,#Generation_IV').parent();.
Thanks for your help, patrick and Malvolio! The corrected code was added to my original post.
2

Both value and this point to the current item in the iteration, but that isn't your problem. Your problem is that the item returned by [] on a jQuery object isn't a jQuery object. You could do this:

$(genH3Array[i]).next() 

Comments

2

Adding to what @patrick dw said: once you get the right selector, you can use the following syntax:

var getH3Array = ('#Generation_II,#Generation_III,#Generation_IV').parent().each(function() {
    $(this); // this references the dom element matched, so:

    if($(this).next().attr("align") == "center") {
         // do something here
    }
});

Comments

0

I think what you want is

var array = $("#c1, #c2, #c3").parent();
$.each(array, function(){
    console.log($(this).next());
});

In $.each callback, the this variable point to the current element. If you are iterating through a jquery array like what you have, it will be iterating through the dom elements not jQuery objects, so you need to get the jQuery objects corresponding to them by using $(this).

jQuery.each

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.