Reputation: 163
I am building online courses out of webpages which are setup in this way:
<h1>Chapter 1</h1>
<p>Welcome to the chapter 1 overview.</p>
<h2>Unit A</h2>
<p>This is the first unit of the first chapter.</p>
<h1>Chapter 2</h1>
<p>This is the chapter 2 overview</p>
In order to automate the task of converting these webpages to online courses, I need to do 2 things:
document.body.innerHTML = "<div align='left' id='mySpecial'></div>" + document.body.innerHTML;
myHeadings = document.querySelectorAll("h1, h2, h3, h4");
for(var i=0; i<=myHeadings.length-1; i++){
if (myHeadings[i].outerHTML.includes('h1') == true){
document.getElementById('mySpecial').innerHTML += '<br>>H1: '+myHeadings[i].innerText
}
if (myHeadings[i].outerHTML.includes('h2') == true){
document.getElementById('mySpecial').innerHTML += '<br>>-->H2: '+myHeadings[i].innerText;
}
if (myHeadings[i].outerHTML.includes('h3') == true){
document.getElementById('mySpecial').innerHTML += '<br>>-->-->H3: '+myHeadings[i].innerText;
}
}
The above script creates an array containing each H1, H2 and H3 in the order that they occur then adds a table of contents to the beginning of the document.
document.body.innerHTML = "<div align='left' id='mySpecial'></div>" + document.body.innerHTML;
myHeadings = document.querySelectorAll("h1, h2, h3, h4");
for(var i=0; i<=myHeadings.length-1; i++){
if (myHeadings[i].outerHTML.includes('h1') == true){
document.getElementById('mySpecial').innerHTML += '<br>>H1: '+myHeadings[i].innerText
document.getElementById('mySpecial').innerHTML += EVERYTHING_BETWEEN(myHeadings[i], myHeadings[i+1])
}
if (myHeadings[i].outerHTML.includes('h2') == true){
document.getElementById('mySpecial').innerHTML += '<br>>-->H2: '+myHeadings[i].innerText;
document.getElementById('mySpecial').innerHTML += EVERYTHING_BETWEEN(myHeadings[i], myHeadings[i+1])
}
if (myHeadings[i].outerHTML.includes('h3') == true){
document.getElementById('mySpecial').innerHTML += '<br>>-->-->H3: '+myHeadings[i].innerText;
document.getElementById('mySpecial').innerHTML += EVERYTHING_BETWEEN(myHeadings[i], myHeadings[i+1])
}
}
Does anyone know of a solution?
Upvotes: 1
Views: 204
Reputation: 35512
Assuming that all your headers have the same parent element (which they do in your example), this function will work:
function EVERYTHING_BETWEEN (el1, el2) {
var siblings = Array.from(el1.parentNode.children); // get an array of all siblings
var between = siblings.slice(siblings.indexOf(el1) + 1, siblings.indexOf(el2)); // get the subarray of elements between
return between.map(v => v.outerHTML).join(""); // replace each with their outerHTML and join
}
Upvotes: 1