Replace HTML div into text element with JavaScript?

For this, use document.querySelectorAll(). With that, also use the getElementsByClassName(). Following is the code −


 Live Demo

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initialscale=1.0">
<link rel="stylesheet" href="//">
<script src=""></script>
<script src=""></script>
<h2 class="uniqueId"> My Name is John </h6>
<div class="my-main-div-class">
<div class="div-class-2">
<span>My h6 value must be here...</span>
<div class="div-class-end">
<span>My h6 value must be here...</span>
<span>My h6 value must be here...</span>
   const allSpan = document.querySelectorAll('.my-main-div-class span'),
   repaceAboveText =
   for (var newElement of allSpan){

To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” in VS Code editor.


This will produce the following output −