aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes getting a unique list of elements with jquery Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "getting a unique list of elements with jquery" Watch "getting a unique list of elements with jquery" New topic
Author

getting a unique list of elements with jquery

Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

I have several selects and several sets of checkboxes on my form. Some of them are marked with class="foo"

I want to get a unique list of all the elements of class "foo" and since there are multiple checkboxes with class of "foo", I get duplicates if I do something like



and I tried


which of course won't work because each checkbox is unique even though it has the same name.

So, how do I get a collection/array of unique form elements?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61417
    
  67

What do you mean you get "duplicates?"


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

I mean that if I have 10 checkboxes in the same group and they all have name = "ppp" and class="foo" then my array of form elements will have 10 items and I only want one. I want a unique array of names of these elements that have class="foo"

so in my wish, my array will have one thing in it: The string "ppp"

if html's checkbox element had a parent, then I could call that class="foo" and be on my way.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61417
    
  67

If they all have the same name, just grab the name of the first element in the wrapped set.
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

Bear Bibeault wrote:If they all have the same name, just grab the name of the first element in the wrapped set.


well, if I have two selects of this class="foo" and two sets of checkboxes with class = "foo", then there are four different names.

if I do var foos = $(.foo);

then I have an array of 12 elements (2 selects and 10 checkboxes)


As you said, I'd like to grab the first element of each checkbox group, but if I have two checkbox groups, I don't know how to get anything but an undifferentiated array of checkboxes plus a few selects

I've moved to just trying for getting an array of checkboxes


Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

I thought I had it with first-child, but all my checkboxes are first children:

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61417
    
  67

Checkbox groups have different names. You don't need to get all the ".foo"'s at once. If you have a copy of my book, you should read chapter 2 again to learn how to make more fine-grained selections. Remember you can also use a combination of complex selectors and the .filter() method to hone a wrapped set to whatever you want.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: getting a unique list of elements with jquery