3

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

3
  • What exactly do you want to do with the "subTerms" after you fetch em? Commented Aug 13, 2009 at 21:57
  • the subTerms are actually exclusively dropdowns so I just want to get their values. Commented Aug 13, 2009 at 21:59
  • Could you give a HTML example of some terms for us? Its hard to do this stuff blind Commented Aug 13, 2009 at 22:23

5 Answers 5

6

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');
});
Sign up to request clarification or add additional context in comments.

1 Comment

.children() assumes direct descendants right? perhaps .find() is what you meant?
2

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(","));

Comments

1

you can scope the second query call to just look under the found term

$(".myTerms").each(function() { 
  var sub = $(".subTerms", this);
});

2 Comments

nice condensed version of my answer 3 minutes before yours. ;)
I'm a slow typer, what can I say :)
1

Couple of things to point out. $(".myTerm") is not an array - its a jquery object, you can use for loops on it, but each is much better. Inside of an each() function this refers to the DOMElement. Also, if you want to search for more objects INSIDE of a jQuery object you can use $element.find('.subterm') or $('.subTerm', element)

$(".myTerm").each(funciton() {
   // look for any .subTerm that exist underneath of us
   var subTerms = $(".subTerm", this);

   // do something with subTerms as an example....
   var text = [];
   subTerms.each(function() {
     // .val() assuming its an option element
     // .text() might be better.
     text.push($(this).val()); 
   });

   alert(text.join(";"));
});

Comments

0

If you don't want to change your code, then you can use the following

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.