Search code examples
jquerychaining

Jquery chian within chian


I have a piece of code which I have chained like this:

$('iframe').contents().find('item').text();

I would like to add something inside the chain like this:

$('iframe').contents().find(   $('item').next().text()   );

This doesn't work for me but Im pretty sure I once saw it done like this, but I can't remember the correct way. So help on this is appreciated. Please note the regular way (chaining everything) doesn't work in my script that's why I have to result to this method.

EDIT This is the code Im working:

    var $me = $('#iframe').contents().find('body *:eq(285)'),
        crazyNumber = 99999999,
        allOfYou = [
            { 
                elements: $me.parentsUntil('.list'),
                target: $me.parentsUntil('.list').parent()
            },
            { 
                elements: $me.nextUntil('.list'),
                target: $me.nextUntil('.list').andSelf().filter(':last').next()
            },
            { 
                elements: $me.prevUntil('.list'),
                target: $me.prevUntil('.list').andSelf().filter(':last').prev()
            }
        ],
        sorted = allOfYou.sort(function (objA, objB) {
            var a = objA.elements.length + 1 || crazyNumber,
                b = objB.elements.length + 1 || crazyNumber;
            return a - b;
        });



  alert(sorted[0].target.text());

This will not work because .list is also in iframe. So for instance

$me.parentsUntil('.list')

Should actually be

$me.parentsUntil('#iframe').contents().find('.list')

This will not work because there are multiple .list Jquery will find a different .list every other time. So I need it to say something like:

$('#iframe').contents().find( $('body *:eq(285)').parentsUntil('.list') )

I have to put everything in the find selector


Solution

  • You can use .each():

    $('iframe').contents().find('item').each(function() {
        $(this).parentsUntil('.list')...
    });