henZa
henZa

Reputation: 73

How can I attach a function to JQuery's find() function?

I have an Array of elements I wish to search for a class within. Once found I then want to attach a function so I can manipulate child elements. My research has led me to the each loop and the find function. I want to do something like this:

$(arrayOfElems).each(function( i ) {
    $(this).find('.something').function() {
        console.log('found element: '+$(this));
    }
}

Excuse the bad code!

Upvotes: 0

Views: 79

Answers (3)

Jack
Jack

Reputation: 526

Depending on how your code is structured you may need an additional loop.

   $("ul.StuffToloop").each(function (i, item) {
                if (item.classList == "ClassToDoThings") {//first check your array of elements
                   $( item).append( "<p>Test</p>" );//if matched add your function that does things to child elements
                    }
                }

Upvotes: 1

renakre
renakre

Reputation: 8291

$(arrayOfElems).each(function( i ) {
    ManipulateItem($(this).find('.something'));
 }

 function ManipulateItem(item){
       //do stuff
 }

Upvotes: 0

suvroc
suvroc

Reputation: 3062

Use .each function https://api.jquery.com/each/

$(this).find('.something').each(function( index ) {
  console.log( index + ": " + $( this ).text() );
});

Upvotes: 2

Related Questions