1

I am struggling to dynamically create tags for each Subheading (h2 element) I have in my blog, and then fill those tags with the text of the Subheading.

This is what I have tried so far:

<script>

            const subheadings = document.querySelectorAll("h2");
            subheadings.forEach(function(x) {
              document.getElementById("contents").innerHTML=x;
              <a href='#' id="contents"></a>
            });
              
</script>

This resulted in nothing appearing.

Any help or advice in which direction to look is greatly appreciated.

*** EDIT ***

I have updated the code to an answer that was given in the responses.

<div class="col-3" id="contents-table">
          <script>

            const subheading = document.querySelectorAll('h2');
            subheading.forEach(function(x) {              
              let xbe = x.innerText.split(' ');
              for (let i = 0; i<xbe.length;i++) {
              const div = document.getElementById('contents-table');
              let a = document.createElement('a');
              a.innerText = xbe[i];
              console.log( a.innerText); 
              div.append(a);
              }
                });
              
          </script>
        </div>

Hopefully, this gives all of the necessary information.

It however still doesn't display the text of the h2 elements but I think the logic is correct.

1
  • Can you explain a bit more what exactly you want to do? And share more code? Commented Dec 11, 2022 at 18:48

1 Answer 1

1

html:

<h2>
    test example two!
  test example two!
  test example two!
</h2>
<div class="container"></div>

css:

.holder {
 
  width:1200px;
  height:20px;
  display: flex;
}

js:

const subheading = document.querySelectorAll('h2');
const cont = document.querySelector('.container')
 subheading.forEach(function(x) {  
   cont.innerHTML = ''
   let xbe = x.innerText.split(' ');
   for (let i = 0; i<xbe.length;i++) {
   const test = document.createElement('div');
     test.classList.add('holder')
     let a = document.createElement('a');
     a.href=''
     a.innerText = xbe[i];
     console.log(a); 
     test.append(a);
     cont.append(test)
  }
   
    });
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your response. I understand the logic of the code so thank you this really clarifies things.
it functions as a holder for your iteration of the tags, its not necessary to assign that, you don't have to do that, I would like to organize my code in div when I want to do css that way its much easier! glad I can help!!!
Thanks Hank, it still does not display but I think your logic is correct. Is there some more code I should present to help people fully understand my question?
I have edited the code to show you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.