Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

getting a unique list of elements with jquery

 
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 67595
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean you get "duplicates?"
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67595
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought I had it with first-child, but all my checkboxes are first children:

 
Bear Bibeault
Sheriff
Posts: 67595
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic