Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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");
    }
});
like image 350
Aaron Avatar asked Aug 15 '12 18:08

Aaron


People also ask

How does callback work in jQuery?

jQuery Callback Functions However, with effects, the next line of code can be run even though the effect is not finished. This can create errors. To prevent this, you can create a callback function. A callback function is executed after the current effect is finished.

Which of the following options is are supported by jQuery callback?

Callbacks() supports a number of methods including callbacks. add() , callbacks. remove() , callbacks. fire() and callbacks.

What jQuery find returns?

jQuery find() Method The find() method returns descendant elements of the selected element. A descendant is a child, grandchild, great-grandchild, and so on. The DOM tree: This method traverse downwards along descendants of DOM elements, all the way down to the last descendant.

Which callback function is passed the returned data?

The function to which the callback is passed is often referred to as a higher-order function. Conversely, Higher-Order Functions operate on other functions by either taking them as arguments or by returning them.


2 Answers

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");
});
like image 89
Mark Coleman Avatar answered Sep 28 '22 15:09

Mark Coleman


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();
like image 21
gray state is coming Avatar answered Sep 28 '22 14:09

gray state is coming