Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes HTML, CSS and JavaScript and the fly likes Trouble with the jQuery find method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Trouble with the jQuery find method" Watch "Trouble with the jQuery find method" New topic
Author

Trouble with the jQuery find method

Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2840
    
  11

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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30124
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2840
    
  11

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

Joined: Jan 10, 2002
Posts: 60785
    
  65

Yup, I think it was filter() that you were thinking of.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Trouble with the jQuery find method
 
Similar Threads
Why can't I get \n to work?
How to perform Calculation when a button a pressed in a Frame
getParameterValues and TextArea inputs
create dynamic checkboxes
Looping JCheckBox