Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
Adding Elements with Ids to a Div so I can access the new Elements with getElementById

[Edit: Thanks to Ganesh's answer, it led me on the right track to what the actual problem was. So thanks for that. The problem was in my for loop I have let i = 0, str = "", which meant I was creating a new str for the loop, which was different than the str outside the loop. This meant when I set el.innerHTML = str outside the loop, it was not the str that had been created in the loop.]

I am creating a w3.css dropdown with canvases and labels in it. I am basing it on the example with the title 'Clickable Dropdowns' on the webpage W3.CSS Dropdowns.

<div class="w3-dropdown-click">
    <button onclick="OnClickDoStuff()" class="w3-button w3-black">
        <canvas id="imageMain"></canvas>
        &nbsp;
        <label id="labelMain">Name</label>
    </button>
    <div id="divInnards" class="w3-dropdown-content w3-bar-block w3-border">
        <!-- Stuff inserted with javascript -->
    </div>
</div>

I am adding the elements of divInnards in javascript.

let el = document.getElementById("divInnards");
let str, iStr;

for(let i=0, str = "";i < 24; i++){
    iStr = i.toString();
    str += "<a href=\"#\" class=\"w3-bar-item w3-button\""
                    + " id=\"divLink" + iStr + "\""
                    + " onclick=\"OnClickLink(" + iStr + ")\""
            + ">"
            + "<canvas id=\"divCanvas" + iStr + "\" ></canvas>"
            + "&nbsp;"
            + "<label id=\"divLabel" + iStr + "\">Name</label>"
        + "</a>";
}
el.innerHTML = str;

However, then when I try and get divCanvas0 later on, I get null.

let cnv = document.getElementById("divCanvas0"); // cnv === null :(

What am I doing wrong?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

because after laste itration istr becomes 23, try this

let el = document.getElementById("divInnards");
let str, iStr;

for(let i=0;i < 24; i++){
    iStr = i.toString();
    str +=  "<a href=\"#\" class=\"w3-bar-item w3-button\""
                    + " id=\"divLink" + iStr + "\""
                    + " onclick=\"OnClickLink(" + iStr + ")\""
            + ">"
            + "<canvas id=\"divCanvas" + iStr + "\" ></canvas>"
            + "&nbsp;"
            + "<label id=\"divLabel" + iStr + "\">Name</label>"
        + "</a>";
        
        el.innerHTML = str;
  



}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!