The HTML DOM nodeValue property returns/sets a string corresponding to the value of the node.
Following is the syntax −
Returning string value
Node.nodeValue
Here, the return value can be the following −
- Value as ‘null’ for document nodes & element nodes
- Value as ‘value’ of the attribute for attribute nodes
- Value as content for text nodes and comment nodes
Set nodeValue to a string value
Node.nodeValue = string
NOTE: Whitespaces are considered as text nodes only.
Let us see an example of HTML DOM nodeValue property −
Example
<!DOCTYPE html>
<html>
<head>
<title>HTML DOM nodeValue</title>
<style>
form {
width:70%;
margin: 0 auto;
text-align: center;
}
* {
padding: 2px;
margin:5px;
}
input[type="button"] {
border-radius: 10px;
}
ul{
width: 30%;
margin: 0 auto;
}
</style>
</head>
<body>
<form>
<fieldset>
<legend>HTML-DOM-nodeValue</legend>
<h3>Students</h3>
<ul>
<li>Adam</li>
<li>Alex</li>
<li>Bina</li>
<li>Eden</li>
<li>Rajesh</li>
<li>Zampa</li>
</ul>
<input type="button" onclick="checkForBina()" value="Confirm for Bina">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
var divDisplay = document.getElementById("divDisplay");
var studentList = document.getElementsByTagName("li");
var status = 'not Present';
function checkForBina() {
for(var i=0; i<studentList.length; i++){
if(studentList[i].childNodes[0].nodeValue === 'Bina')
status = 'Present';
}
divDisplay.textContent = 'Bina is '+status;
}
</script>
</body>
</html>Output
Before clicking ‘Confirm for Bina’ button −

After clicking ‘Confirm for Bina’ button −
