Jquery qiz clarification request | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Jquery qiz clarification request

In the qiz of the new Jquery couse, the answer to the question asking how to access the children p elements of the "demo" element is: $("#demo p") But this will select all descendants p elements of the "demo" element. shouldn't the answer be: $("#demo > p") as, this access all CHILDREN elements of "demo" as asked in the qiz???? Oops Spoiler alert....

29th Dec 2016, 3:53 PM
seamiki
seamiki - avatar
4 Answers
+ 1
@ Abhilash k. V. il Grandchildren==children there should be no need to differentiate children from descendants: therefore there should be no need for the different sintax $("demo p") CHILDREN + GRANDCHILDREN and $("demo > p") CHILDREN Since this difference exists I don't find your answer correct. Thx anyway for sharing your thoughts
4th Jan 2017, 5:16 PM
seamiki
seamiki - avatar
+ 1
The .children() method differs from .find() in that .children() only travels a single level down the DOM tree while .find() can traverse down multiple levels to select descendant elements (grandchildren, etc.) as well. Based on this Logic Children "p" elements are those next in a single level down the Parent "demo" and they can be accessed via $("demo > p"). Descendants are all children on multiple levels down the Parent "demo" and they can be accessed via $("demo p") If this is the required answer, the question should relate to the DESCENDANT p element rather than the CHILDREN. It's semantics but it's an educational course
4th Jan 2017, 6:20 PM
seamiki
seamiki - avatar
0
$("demo > p") means direct descendants i.e. only direct children. $("demo p") means children + grandchildren +... . Even grandchildren are children in my eyes.
4th Jan 2017, 4:16 PM
Abhilash Kumar Vedwan
Abhilash Kumar Vedwan - avatar
0
In jQuery children involve every dom object inside. Hence for children we use $("demo p"), and when we use $("demo > p") we mean one level descendants. Besides even grandchildren have children in the name ;). If u are not satisfied, what should u suggest naming $("demo p") as?
4th Jan 2017, 5:23 PM
Abhilash Kumar Vedwan
Abhilash Kumar Vedwan - avatar