HTML DOM lastChild Property


The HTML DOM lastChild property returns the last element node as node object of the specified node.

NOTE − This property does not ignore text and comment nodes

Syntax

Following is the syntax −

Returning lastChild node

nodeList.lastChild

Example

Let us see an example for HTML DOM lastChild property −

 Live Demo

<!DOCTYPE html>
<html>
<head>
<title>HTML DOM lastChild</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-lastChild</legend>
<h3>Grocery Store</h3>
<ul id="dayList">
<li>Monday</li>
<li>Tuesday</li>
<li>Wednesday</li>
<li>Thursday</li>
<li>Friday</li>
<li>Saturday</li>
<li>Sunday</li></ul>
//Do not add whitespace before closing <ul> as lastChild will take whitespace as last child
<input type="button" onclick="getTimmings()" value="Get Sale Day">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
   var divDisplay = document.getElementById("divDisplay");
   var dayList = document.getElementById("dayList");
   function getTimmings() {
      divDisplay.textContent = 'Sale on last day of week: '+dayList.lastChild.textContent;
   }
</script>
</body>
</html>

Output

This will produce the following output −

Before clicking ‘Get Sale Day’ button −

After clicking ‘Get Sale Day’ button −

Updated on: 30-Jul-2019

40 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements