Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getting a unique list of elements with jquery

 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64623
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean you get "duplicates?"
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64623
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If they all have the same name, just grab the name of the first element in the wrapped set.
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought I had it with first-child, but all my checkboxes are first children:

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64623
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic