Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I select items in an array using jQuery?

Tags:

jquery

I have this line which successfully select all elements of class"myclass" on my page:

var myTerms = $(".myTerm");

The above line works, myTerms is an array of all the myTerm, each of which contain "subTerms".

Then as i iterate over each myTerm, I want to use jQuery to return an array of the "subTerms" within that myTerm.

I tried:

var myTerms = $(".myTerm");
for (var i = 0; i < myTerms.length; i++) {
    var myTerm = queryTerms[i];
    subTerms = myTerm.$("subTerms");

But that's a no go. Is jQuery even intended for this or is it better to fall back to plain old java script.

EDIT

subTerm is the className of elements inside of myTerm

like image 548
Matt Avatar asked May 15 '26 04:05

Matt


2 Answers

Kinda confused on what the subterms object is, but I'll give it a shot.

$('.myTerm').each(function(){
  subTerms = $(this).children();
});

** Edit **

If you want the select options, do

$('.myTerm').each(function(){
  subTerms = $(this).children('.subTerm option');
});
like image 149
Derek Gathright Avatar answered May 16 '26 18:05

Derek Gathright


I'm not sure what you're asking. It sounds like you want to match all items which have the class myterm + another class in your list, e.g. <li class="myterm subterm1">

I'd handle this in a selector. jQuery selectors can match on multiple classes if they're comma sparated, e.g. "subterm1, subterm2, subterm3" so if you join them into a string, you can filter on it.

var result = $(".myterm").filter(myterms.join(","));
like image 26
Jon Galloway Avatar answered May 16 '26 19:05

Jon Galloway



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!