Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trouble with the jQuery find method

 
Greg Charles
Sheriff
Posts: 2993
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm doing something wrong with searching inside a context. I'd expect something like $("input[name=somename]") to return the same set as $("input").find("[name=somename]") in all cases. However, on Bear's Operations Lab Page, I typed in:

$("input[name=checkboxes]")

Results:

4 matching elements:
INPUT#checkbox1
INPUT#checkbox2
INPUT#checkbox3
INPUT#checkbox4

Then I tried:
$("input").find("[name=checkboxes]")

Results:

0 matching elements:


I also tried $("[name=checkboxes]",$("input")), and also got 0 results. Where am I going wrong?


For reference:

$("input")

Results:

8 matching elements:
INPUT#aTextField
INPUT#radioA
INPUT#radioB
INPUT#radioC
INPUT#checkbox1
INPUT#checkbox2
INPUT#checkbox3
INPUT#checkbox4


 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg,
find() is supposed to match a descendant (child, grandchild, etc) of the matched node.

The following finds a bunch of li.


Since name="checkboxes" is an attribute on the attribute you have already matched and not a child, find() can't find it. But the original can because you are asking for attributes on the current element.
 
Greg Charles
Sheriff
Posts: 2993
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, that's right. I knew I must be doing something fundamentally wrong. It's filter I want, right? Sigh, this will all sink in eventually. Thanks!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65227
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, I think it was filter() that you were thinking of.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic