JQuery find - can i use callback?

So, I'm trying to figure out if I can call the function inside find () as shown below, but I am not getting anything on 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");
    }
});
+5
source share
2 answers

It looks like you want to .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");
});
+12
source

You need to jQuery each().

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

Read more about jQuery each()in the official documentation

or you can use filter()

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

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

or how is it

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

All Articles