bRaNdOn
bRaNdOn

Reputation: 1082

.find is not working

hi this is my problem im currently looping all the selected element using jquery selector and tried to use .find(Selector) of jquery but i think its not working or is it possible to find an element using this code

 for (var i = 0; i < $('.MainElement').find('.ItemGroup').length; i++) {
            var CurrentSelectedGroup = $('.MainElement').find('.ItemGroup')[i].find('span');
            }

I debugged this code and its returning a unavailable but when i tried to jquery select the element manually its working is it possible to do this??

i need to select the span inside the current element inside the loop

I have searched in google i didnt find any

Upvotes: 0

Views: 80

Answers (3)

Arun P Johny
Arun P Johny

Reputation: 388316

Though eq() as given below works, a better approach will be is to use .each() for iteration, as you are running your selector multiple times in your script

$('.MainElement').find('.ItemGroup').each(function(){
    var CurrentSelectedGroup = $(this).find('span');
})

or at the least cache the value of your selector and then reuse it in your loop

Upvotes: 2

Vlad Cazacu
Vlad Cazacu

Reputation: 1540

Try this as it's much cleaner:

$('.MainElement').find('.ItemGroup').each(function() {
    var CurrentSelectedGroup = $(this).find('span');
});

Upvotes: 1

Balachandran
Balachandran

Reputation: 9637

use eq()

var CurrentSelectedGroup = $('.MainElement').find('.ItemGroup').eq(i).find('span');

NOTE: $('.MainElement').find('.ItemGroup')[i] will return javascript object not jquery

Upvotes: 1

Related Questions