.addChild( "Child_Tag_Name" [, Properties ] [, Position ]) - Add a Child element
.addChild() will append a child element to a parent. It can set attributes to the child using the Properties parameter. And child can be attached to a specific index using Position parameter.
Parameters
Returns
Example 1
Clicking on "Add Child" button will add a child div element to #parent.
JS
HTML
CSS
$.domLoaded(function() { $.get("#add").on("click", function() { //add a div child to #parent $.get("#parent").addChild("div", { "class" : "child", "text" : "Child" }); }); });
Result
Example 2
Using Nedil.Objs of child element which is returned by .addChild() and attach a click event to the new child.
JS
HTML
CSS
$.domLoaded(function() { $.get("#add").on("click", function() { //add a div child to #parent and ch is child Nedil object var ch = $.get("#parent").addChild("div", { "class" : "child", "text" : "Clickable Child" }); ch.on("click", function() { alert("You Clicked on Child"); }); }); });
Result
Example 3
Usually addChild() function will attach the new child as the last one. This example explains how to attach a child at a specified index (position).
JS
HTML
CSS
$.domLoaded(function() { $.get("#add1").on("click", function() { //add a div child to #parent at 2nd position $.get("#parent").addChild("div", 1); }); $.get("#add2").on("click", function() { //add a div child to #parent at 3rd position with properties $.get("#parent").addChild("div", { "text" : "New Child", "class" : "diff" }, 2); }); });
Result
| |||||||||||||||||||||||||||