Definition and Usage firstChild..
The firstChild property returns the first child node of the specified node, as a Node object.
The difference between this property and firstElementChild, is that firstChild returns the first child node as an element node, a text node or a comment node (depending on which one's first), while firstElementChild returns the first child node as an element node (ignores text and comment nodes).
Note: Whitespace inside elements is considered as text, and text is considered as nodes (See "More Examples").
This property is read-only.
Tip: Use the element.childNodes property to return any child node of a specified node. childNodes[0] will produce the same result as firstChild.
Return Value:A Node object, representing the first child of a node, or null if there are no child nodes.
Syntax : node.firstChild
Definition and Usage firstElementChild..
The firstElementChild property returns the first child element of the specified element.
The difference between this property and firstChild, is that firstChild returns the first child node as an element node, a text node or a comment node (depending on which one's first), while firstElementChild returns the first child node as an element node (ignores text and comment nodes).
This property is read-only.
Tip: Use the children property to return any child element of a specified element. children[0] will produce the same result as firstElementChild
Syntax : element.firstElementChild
Differents way how to declare .
CODING:The firstChild property returns the first child node of the specified node, as a Node object.
The difference between this property and firstElementChild, is that firstChild returns the first child node as an element node, a text node or a comment node (depending on which one's first), while firstElementChild returns the first child node as an element node (ignores text and comment nodes).
Note: Whitespace inside elements is considered as text, and text is considered as nodes (See "More Examples").
This property is read-only.
Tip: Use the element.childNodes property to return any child node of a specified node. childNodes[0] will produce the same result as firstChild.
Return Value:A Node object, representing the first child of a node, or null if there are no child nodes.
Syntax : node.firstChild
Definition and Usage firstElementChild..
The firstElementChild property returns the first child element of the specified element.
The difference between this property and firstChild, is that firstChild returns the first child node as an element node, a text node or a comment node (depending on which one's first), while firstElementChild returns the first child node as an element node (ignores text and comment nodes).
This property is read-only.
Tip: Use the children property to return any child element of a specified element. children[0] will produce the same result as firstElementChild
Syntax : element.firstElementChild
Differents way how to declare .
<!DOCTYPE html>
<html>
<body>
<h4 id="h4">Soft Languages:</h4>
<ul id="lan"><li>JAVA</li><li>PYTHON</li></ul>
<button onclick="con()">First Contant</button>
<button onclick="nod()">First Node Name</button>
<h4 id="h42">Web Languages:</h4>
<select id="Select" size="4"><option>HTML</option><option>CSS</option><option>JAVA SCRIPT</option><option>XML</option></select><br><br>
<button onclick="con2()">First Contant</button>
<button onclick="nod2()">First Node Name</button>
<p><strong>Note:</strong> Whitespace inside elements is considered as text, and text is considered as nodes.If you add whitespace before the first LI element, the result will be "undefined".</p>
<script>
function con() {
var x = document.getElementById("lan").firstChild.innerHTML;
document.getElementById("h4").innerHTML += x;
}
function nod() {
var y = document.getElementById("lan").firstChild.nodeName;
document.getElementById("h4").innerHTML += y;
}
function con2() {
var a = document.getElementById("Select").firstChild;
document.getElementById("h42").innerHTML += a.text;
}
function nod2() {
var b = document.getElementById("Select").firstChild;
document.getElementById("h42").innerHTML += b.nodeName;
}
</script>
</body>
</html>
<html>
<body>
<h4 id="h4">Soft Languages:</h4>
<ul id="lan"><li>JAVA</li><li>PYTHON</li></ul>
<button onclick="con()">First Contant</button>
<button onclick="nod()">First Node Name</button>
<h4 id="h42">Web Languages:</h4>
<select id="Select" size="4"><option>HTML</option><option>CSS</option><option>JAVA SCRIPT</option><option>XML</option></select><br><br>
<button onclick="con2()">First Contant</button>
<button onclick="nod2()">First Node Name</button>
<p><strong>Note:</strong> Whitespace inside elements is considered as text, and text is considered as nodes.If you add whitespace before the first LI element, the result will be "undefined".</p>
<script>
function con() {
var x = document.getElementById("lan").firstChild.innerHTML;
document.getElementById("h4").innerHTML += x;
}
function nod() {
var y = document.getElementById("lan").firstChild.nodeName;
document.getElementById("h4").innerHTML += y;
}
function con2() {
var a = document.getElementById("Select").firstChild;
document.getElementById("h42").innerHTML += a.text;
}
function nod2() {
var b = document.getElementById("Select").firstChild;
document.getElementById("h42").innerHTML += b.nodeName;
}
</script>
</body>
</html>
Comments
Post a Comment