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

210
Views
How can I create and modify multiple SVGs dynamically

I am adding multiple SVGs dynamically, then modifying each of them. Without an event listener, the map was not being seen. Now, however, the event listener appears to create multiple instances of the last loop, instead of one for each loop, only the last instance gets modified, but multiple times with the same mygroup,svgID.

for (var i=0; i<path.length; i++) {
    var mygroup = path[i], svgID = "svgMap" + i
    const iSVG = document.createElement("object")
    document.getElementById("summary-mygroup").appendChild(iSVG)
    iSVG.id = svgID
    iSVG.type = "image/svg+xml"
    iSVG.data = "Maps/mygroup_basemap.svg"
    iSVG.addEventListener('load', function(){
        createMap(mygroup,svgID)
    })
}
about 4 years ago ยท Juan Pablo Isaza
1 answers
Answer question

0

TL;DR:

use const instead of var

const mygroup = path[i], divID = "div" + i, svgID = "svgMap" + i

What you are seeing is due to function() using mygroup, divID , and svgID form the loop's scope which keeps updating until the functions execute (all with the latest value). This happens because the same variable is used.

var and const/let do not behave the same

var scopes to the function, whereas let/const are scoped to the block. (var also gets hoisted, but that's less related to the issue)

so if you run:

for (var i=0; i < 3; i++){
    var b = i
  setTimeout(function(){console.log(b)},1)// ๐Ÿ˜ก 2,2,2
}
console.log("B:", b) // ๐Ÿ˜ฌ 2

you wouldn't expect to have console.log("B:", b) run without an error, but it does, because the scope of var exists outside of the function.

whereas if you use let or const

for (var i=0; i < 3; i++){
  let b = i;
  setTimeout(function(){console.log(b)},1)// ๐Ÿ‘ 0,1,2
}
console.log("B:", b)  // ๐Ÿ‘ throws error

you will have expected behaviour, including an error on the console.log

And because it is a function-vs-block-scope issue, you could move the entire functionality inside a function and call it, which will lock the scope to the function:

for (var i=0; i < 3; i++){
  (function(){
    var b = i
    setTimeout(function(){console.log(b)},1)// ๐Ÿ‘ 0,1,2
  })()
}
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!