1

Hello I have JSON file with this structure (I have 249 countries, all with iso codes) and as you can see I want to declare iso code as var iso, and visitors, as var visitors, and then if visistors have more than 1, and less than 50, fill SVG element where ID=iso with #94d31b, but it doesn't work... I don't know what to do now. and before I do "if" I want to fill Canada, but console says that ca does not exist, but I have this element in SVG, and when I delete this line, and put this line into if, then I have no error, so if doesn't work, because if he did, he should say that "ca" does not exist.

1 Answers1

0

You're looping through your data incorrectly. You actually want to loop through the data.iso_countries array. Try:

$.getJSON("results.json", function(data) {
    data = data.iso_countries; // the array we really want
    for(var key in data) {

As for how to access individual parts of the SVG image, see this question.

Community
  • 1
  • 1
Blazemonger
  • 90,923
  • 26
  • 142
  • 180
  • It works, because now if works either, but this I have in console TypeError: document.getElementById(...) is null document.getElementById("iso").style.fill="#94d31b"; because I want to get ID by iso, because iso has actual isocode document.getElementById(iso).style.fill="#94d31b"; if I put document.getElementById(ca).style.fill="#94d31b"; and then console says that ca does not exist, but in SVG Canada has id="ca" :| – user3184915 Jan 13 '14 at 14:07
  • Added a link to an existing question that should help you with that. – Blazemonger Jan 13 '14 at 15:28
  • Thank you :) I updated my original post with new data from that link, because now, console says that svgMap is undefined... TypeError: svgMap is undefined svgMap.getElementById(iso).style.fill="#94d31b"; – user3184915 Jan 13 '14 at 15:49
  • That should be a fairly straightforward fix. If your code has multiple problems, try to narrow them down and post them as separate questions. – Blazemonger Jan 13 '14 at 15:58
  • it works! haha but now I don't know why, because if I use – user3184915 Jan 14 '14 at 06:12
  • I'm not sure how the DOM views `` versus ``, but I can't say I'm surprised, either. I think it interprets `` in the same way as ` – Blazemonger Jan 14 '14 at 14:32