I'm trying to create a script that will enclose each chapter of writing in a div element, so that I can automate tabination on posts without having to manually implement it each time. Take something simple, like:
Chapter 1
bla bla bla bla
bla bla bla bla
bla bla bla bla
Chapter 2
bla bla bla bla
bla bla bla bla
Chapter 3
bla bla bla bla
bla bla bla bla
bla bla bla bla
bla bla bla bla
Chapter 4
bla bla bla bla
bla bla bla bla
And turn it into:
Chapter 1
bla bla bla bla
bla bla bla bla
bla bla bla bla
Chapter 2
bla bla bla bla
bla bla bla bla
Chapter 3
bla bla bla bla
bla bla bla bla
bla bla bla bla
bla bla bla bla
Chapter 4
bla bla bla bla
bla bla bla bla
(And then I'll get the tabination done, but I feel it'll be much simpler once I have this part working.)
I think something similar to -
h2.insertAdjacentHTML("beforebegin", "...would work (I'll have to create the first starting
Chapter X
bla bla bla bla
...which isn't what I'm going for.
I did find somebody experiencing a similar problem here, but I'm about as inexperienced as you can get with Javascript, so I struggled to replicate the answer given without a completed example to study from.
This is what I'm currently working with:
// 1. Count total amount of chapters in document
var chapterCount = document.querySelectorAll("h2").length;
// 2. Give all tags a unique ID, since I can't seem to use insertAdjacentHTML en masse
for (let i = 0; i < chapterCount; i++) {
document.querySelectorAll("h2")[i].setAttribute("id", "section-" + (i + 1));
}
// 3. This one doesn't work, but you can see the logic I'm going for. Select each individually, then run the insertAdjacentHTML command on loop until there's no more chapters.
for (let i = 0; i < chapterCount; i++) {
document.querySelectorAll("#section-") + [i].insertAdjacentHTML("beforebegin", "
For Step 3, trying to follow the logic of the other question linked alongside this one as well, I fiddled around with solutions somewhat like below (which doesn't solve the problem of applying it to every
tag, I just needed to know if it worked at all):
const targetElement = document.querySelector("h2")
const pseudoContainer = document.createElement('div');
pseudoContainer.innerHTML = "
const targetElement = document.querySelector("h2")
const pseudoContainer = document.createElement('div');
pseudoContainer.innerHTML = "But the end result always just gives me [object HTMLDivElement] as a return rather than anything I've inputted. (And it's not the weird incomplete tags causing it, because changing to random text returns [object Text] instead.) Inserting pseudoContainer.innerHTML doesn't work either, since it returns a
above the header like I ran into earlier.Any pointers on where to go from here? I'm sure I'm missing something incredibly obvious, but I tend to avoid using Javascript whenever possible because I lack the experience to understand the terminology used when looking in on other people's discussions. This script will save me a lot of time going forward however, so I'm doing what I can to learn everything one step at a time.
Thank you in advance!!
Privacy & Cookie Consent
We use cookies to ensure the best experience on our website. This includes analytics, personalization, and marketing purposes. Some cookies are essential for the website to function properly.
By clicking "Accept", you consent to our use of cookies. You can read more about how we use cookies and how you can change your preferences in our Privacy Policy.