[jQuery] Re: Selector questions
On Apr 16, 2009, at 8:26 AM, Dragon-Fly999 wrote: Instead of using $(this).parent().parent().find(':checkbox'), is there a way to select using the following rule: Go up the hierarchy (regardless of how many parents there are) until the first a is found, then give me a list of all the checkboxes that are the descendants of the div right after the a. Unfortunately, given your markup, you can't do that, since a is not an ancestor of $(this). It's a previous sibling of one of the ancestor divs. Leonardo's suggestion to give the wrapper div a class name is what I'd also suggest. You could then use .closest('div.wrapper') as he recommends, or .parents('div.wrapper:first') --Karl Karl Swedberg www.englishrules.com www.learningjquery.com
[jQuery] Re: Selector questions
Thanks, Karl. Your suggestions work fine. I just started using JQuery and found the selectors very powerful. I was wondering if the selectors that you suggested can be less dependent on the number of divs in that section of the HTML. Instead of using $(this).parent().parent().find(':checkbox'), is there a way to select using the following rule: Go up the hierarchy (regardless of how many parents there are) until the first a is found, then give me a list of all the checkboxes that are the descendants of the div right after the a. Similarly, instead of using $(this).parent().parent().prev(), is there a way to select using the following rule: Go up the hierarchy (regardless of how many parents there are) and return the first a. The reason that I asked these questions is because I can see myself adding more div's in that section of the HTML. On Apr 15, 10:31 pm, Karl Swedberg k...@englishrules.com wrote: On Apr 15, 2009, at 6:24 PM, Dragon-Fly999 wrote: Hi, I have a couple of questions about selectors. I have the following HTML: = Some a elements and input elements here. ... ... a id=info-1-headingInformation Type 1/a div div input class=cat id=first type=checkbox/label for=firstFirst/label /div div input class=cat id=mid type=checkbox/label for=midMiddle/label /div div input class=info1-cat id=last type=checkbox/label for=lastLast/label /div /div ... ... More a elements and input elements here. = Question 1: In the click handler of the first checkbox, I would like to get the first, middle, and last checkboxes (but not the other checkboxes on the page). What selector should I use? There are a number of options, but this will do the trick: $(this).parent().parent().find(':checkbox') Question 2: In the click handler of the first checkbox, I would like to get the first a element that it finds traversing up the hierarchy (i.e. the a with id=info-1-heading). What selector should I use? Thank you. $(this).parent().parent().prev() or combined: $ (this ).parent ().parent ().find(':checkbox').doSomething().end().prev().doSomethingElse(); and a bit more readable: $(this) .parent() .parent() .find(':checkbox').doSomething() .end() .prev().doSomethingElse(); --Karl Karl Swedbergwww.englishrules.comwww.learningjquery.com
[jQuery] Re: Selector questions
Maybe is easier if you put a class (wrapper) in your div that wrap all other divs, and then you could use: $(this).closest('div.wrapper').find(:checkbox); and $(this).closest('div.wrapper').prev(); On Thu, Apr 16, 2009 at 09:26, Dragon-Fly999 dragon-fly...@hotmail.comwrote: Thanks, Karl. Your suggestions work fine. I just started using JQuery and found the selectors very powerful. I was wondering if the selectors that you suggested can be less dependent on the number of divs in that section of the HTML. Instead of using $(this).parent().parent().find(':checkbox'), is there a way to select using the following rule: Go up the hierarchy (regardless of how many parents there are) until the first a is found, then give me a list of all the checkboxes that are the descendants of the div right after the a. Similarly, instead of using $(this).parent().parent().prev(), is there a way to select using the following rule: Go up the hierarchy (regardless of how many parents there are) and return the first a. The reason that I asked these questions is because I can see myself adding more div's in that section of the HTML. On Apr 15, 10:31 pm, Karl Swedberg k...@englishrules.com wrote: On Apr 15, 2009, at 6:24 PM, Dragon-Fly999 wrote: Hi, I have a couple of questions about selectors. I have the following HTML: = Some a elements and input elements here. ... ... a id=info-1-headingInformation Type 1/a div div input class=cat id=first type=checkbox/label for=firstFirst/label /div div input class=cat id=mid type=checkbox/label for=midMiddle/label /div div input class=info1-cat id=last type=checkbox/label for=lastLast/label /div /div ... ... More a elements and input elements here. = Question 1: In the click handler of the first checkbox, I would like to get the first, middle, and last checkboxes (but not the other checkboxes on the page). What selector should I use? There are a number of options, but this will do the trick: $(this).parent().parent().find(':checkbox') Question 2: In the click handler of the first checkbox, I would like to get the first a element that it finds traversing up the hierarchy (i.e. the a with id=info-1-heading). What selector should I use? Thank you. $(this).parent().parent().prev() or combined: $ (this ).parent ().parent ().find(':checkbox').doSomething().end().prev().doSomethingElse(); and a bit more readable: $(this) .parent() .parent() .find(':checkbox').doSomething() .end() .prev().doSomethingElse(); --Karl Karl Swedbergwww.englishrules.comwww.learningjquery.com
[jQuery] Re: Selector questions
I didn't know about the closest method and I could use it in my case. Thank you. On Apr 16, 8:33 am, Leonardo K leo...@gmail.com wrote: Maybe is easier if you put a class (wrapper) in your div that wrap all other divs, and then you could use: $(this).closest('div.wrapper').find(:checkbox); and $(this).closest('div.wrapper').prev(); On Thu, Apr 16, 2009 at 09:26, Dragon-Fly999 dragon-fly...@hotmail.comwrote: Thanks, Karl. Your suggestions work fine. I just started using JQuery and found the selectors very powerful. I was wondering if the selectors that you suggested can be less dependent on the number of divs in that section of the HTML. Instead of using $(this).parent().parent().find(':checkbox'), is there a way to select using the following rule: Go up the hierarchy (regardless of how many parents there are) until the first a is found, then give me a list of all the checkboxes that are the descendants of the div right after the a. Similarly, instead of using $(this).parent().parent().prev(), is there a way to select using the following rule: Go up the hierarchy (regardless of how many parents there are) and return the first a. The reason that I asked these questions is because I can see myself adding more div's in that section of the HTML. On Apr 15, 10:31 pm, Karl Swedberg k...@englishrules.com wrote: On Apr 15, 2009, at 6:24 PM, Dragon-Fly999 wrote: Hi, I have a couple of questions about selectors. I have the following HTML: = Some a elements and input elements here. ... ... a id=info-1-headingInformation Type 1/a div div input class=cat id=first type=checkbox/label for=firstFirst/label /div div input class=cat id=mid type=checkbox/label for=midMiddle/label /div div input class=info1-cat id=last type=checkbox/label for=lastLast/label /div /div ... ... More a elements and input elements here. = Question 1: In the click handler of the first checkbox, I would like to get the first, middle, and last checkboxes (but not the other checkboxes on the page). What selector should I use? There are a number of options, but this will do the trick: $(this).parent().parent().find(':checkbox') Question 2: In the click handler of the first checkbox, I would like to get the first a element that it finds traversing up the hierarchy (i.e. the a with id=info-1-heading). What selector should I use? Thank you. $(this).parent().parent().prev() or combined: $ (this ).parent ().parent ().find(':checkbox').doSomething().end().prev().doSomethingElse(); and a bit more readable: $(this) .parent() .parent() .find(':checkbox').doSomething() .end() .prev().doSomethingElse(); --Karl Karl Swedbergwww.englishrules.comwww.learningjquery.com
[jQuery] Re: Selector questions
On Apr 15, 2009, at 6:24 PM, Dragon-Fly999 wrote: Hi, I have a couple of questions about selectors. I have the following HTML: = Some a elements and input elements here. ... ... a id=info-1-headingInformation Type 1/a div div input class=cat id=first type=checkbox/label for=firstFirst/label /div div input class=cat id=mid type=checkbox/label for=midMiddle/label /div div input class=info1-cat id=last type=checkbox/label for=lastLast/label /div /div ... ... More a elements and input elements here. = Question 1: In the click handler of the first checkbox, I would like to get the first, middle, and last checkboxes (but not the other checkboxes on the page). What selector should I use? There are a number of options, but this will do the trick: $(this).parent().parent().find(':checkbox') Question 2: In the click handler of the first checkbox, I would like to get the first a element that it finds traversing up the hierarchy (i.e. the a with id=info-1-heading). What selector should I use? Thank you. $(this).parent().parent().prev() or combined: $ (this ).parent ().parent ().find(':checkbox').doSomething().end().prev().doSomethingElse(); and a bit more readable: $(this) .parent() .parent() .find(':checkbox').doSomething() .end() .prev().doSomethingElse(); --Karl Karl Swedberg www.englishrules.com www.learningjquery.com