1

is there any difference b/w `

$(this).parent().children(".name") 

and

$(this).parent().find(".name")

? Both these function return the same object. When I do

console.log($(this).parent().children(".name"));

and

console.log($(this).parent().children(".name"));

both these function print object of div name on console..

Nitin9791
  • 1,124
  • 1
  • 14
  • 17

2 Answers2

0

Basically .children() look for the first level of child elements at the same time .find() will look for the descendants. This is the basic difference between the both.

Say for example if you have .name in the nested level, then .children() wont return it at the same time .find() would return it.

To know more about it just read .children() and .find().

Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
  • which is better to use when we have to look for only first level of child?? – Nitin9791 Jul 18 '14 at 07:29
  • @Nitin9791 `.children()` would be the best choice here. – Rajaprabhu Aravindasamy Jul 18 '14 at 07:30
  • @ Rajaprabhu Aravindasamy thanks but could you explain why?? – Nitin9791 Jul 18 '14 at 07:31
  • @Nitin9791 .children() would do the searching in a small set of elements, but .find() would perform search over a big collection since it basically looks for deeper level elements. And glad to help..! If you feel anyone of the answers provided here satisfied your need, then try to click on the tick mark available with that answer in order to show your acceptance to the future visitors.. And it is not a compulsion... :) – Rajaprabhu Aravindasamy Jul 18 '14 at 07:34
0

this return all the children that have class name.

console.log($(this).parent().children(".name"));

this returns the first object that meets this class.

$(this).parent().find(".name")