children property in javascript
children property is used to return all child elements within the element.
Using this property, we can get the tags used in the child element as well as its contents.
Syntax
var elem = element.children;
Example Explanation
- In the above example we have created a div (idDiv), it is the parent element.
- Within the parent element, we have added two child elements (div, p).
- When the button is clicked Javascript fun_name function is called.
- In the Javascript function, first we are getting the parent element (document.getElementById("idDiv")) and storing in attr variable.
- Next by declaring children[0] first child element is retrieved and its contents are displayed.
- innerHTML is used for displaying purpose only.