0

Possible Duplicate:
jQuery single selector vs .find()

Is there any difference between $(Parent Child) and $(Parent).find(Child) in jQuery?

You may have multiple children like:

$("div").find("span") which will return all the children spans. But isn't it the same with $("div span")?

Community
  • 1
  • 1
Faraderegele
  • 187
  • 4
  • 14

1 Answers1

5

One very important difference is that $ is overloaded with several meanings and as a consequence vulnerable when used with user-defined strings, while find is not.

E. g.

$('.items .'+location.hash.substr(1)) // very bad idea
$('.items').find('.'+location.hash.substr(1)) // this is OK
Tgr
  • 27,442
  • 12
  • 81
  • 118
  • 1
    That is interesting. Could you elaborate on the vulnerability, maybe with an example is possible? We are using the first syntax a few times in our code and I would be interested in those vulnerabilities. – Nope Aug 29 '12 at 12:30
  • 1
    +1 @FrançoisWahl. I, stylistically, would prefer to do `$('.'+location.hash.substr(1), '.items')` but regardless, I'm having a hard time imagining what kind of vulnerability there is – BLSully Aug 29 '12 at 12:36
  • See [jQuery bug #9521](http://bugs.jquery.com/ticket/9521) for details; unfortunately, the jQuery team was quite half-assed in dealing with the issue. – Tgr Aug 29 '12 at 12:54