Aaron
Aaron

Reputation: 550

jQuery find - can I use a callback?

So I'm trying to figure out if I can call a function inside of find() as below but I'm not getting anything returned to the console. Is this possible with find() or do I need to find an alternative?

$(".tdInner1").find(".block", function () {
    if( $(this).next().hasClass("continuation") ) {
        console.log("yes");
    } else {
        console.log("no");
    }
});

Upvotes: 5

Views: 7149

Answers (2)

gray state is coming
gray state is coming

Reputation: 2107

You need jQuery each().

$(".tdInner1").find(".block").each( function () {
    if( $(this).next().hasClass("continuation") ) {
        console.log("yes");
    } else {
        console.log("no");
    }
});

You can read more about jQuery each() in Official Documentation

or you can use filter()

var block = $(".tdInner1 .block");

var continuation_is_next = block.next().filter(".continuation").prev();

or like this

var continuation_is_next=  $(".tdInner1 .block + .continuation").prev();

Upvotes: 3

Mark Coleman
Mark Coleman

Reputation: 40863

Sounds like you want .each().

$(".tdInner1").find(".block").each(function () {
    if( $(this).next().hasClass("continuation") ) {
        console.log("yes");
    } else {
        console.log("no");
    }
});

Or maybe .filter()

$(".tdInner1").find(".block").filter(function () {
    return $(this).next().hasClass("continuation");
});

Upvotes: 11

Related Questions