-1

How do I increment an integer inside a variable, every time that variable is called? Javascript.

var a=0;

var t=loadXMLDoc("http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist="+x[a].getElementsByTagName("name")[0].childNodes[0].nodeValue+"&api_key=83e386b0ba08735e3dee9b118478e56d&lang=en").getElementsByTagName("bio");

for (i=0;i<20;i++)
{
document.write("<div><button type='button' onclick='document.getElementById("+i+").innerHTML=t[0].getElementsByTagName(\"summary\")[0].childNodes[1].nodeValue;'>Open Bio</button></div>");
}

I'm not sure how I would go about incrementing variable a. I need it to increase by 1 every time variable t is called in the for loop.

When I put all of the code in the for loop I get [object node list] returned so this method is not desired.

2 Answers2

2

If I understood your question correctly, you could define your own getters and setters for the property.

var o = {}
o.__defineSetter__('property', function(value) { this._counter = 0; this._holder = value; })
o.__defineGetter__('property', function() { console.log(this._counter++); return this._holder; })

The counter would be reset every time o.property is assigned a value

o.property = 'Some value'

and then increase every time the property is accessed. So,

console.log(o.property)

would print

0
Some value

to the console. And if you do it again, it would print

1
Some value
Skymt
  • 1,089
  • 9
  • 10
0

After your edit I think I can see your problem now. You will need to put the loadXMLDoc statement in the loop (since you want to load 20 different XML files), but you can't assign the result of every call to the same variable t - as once the button is clicked, the handler will evaluate t and get only the last value.

Instead, use an array:

var bios = []; // empty array
for (var i=0; i<20; i++) {
    var artist = x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue,
        doc = loadXMLDoc("http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist="+artist+"&api_key=83e386b0ba08735e3dee9b118478e56d&lang=en"),
        bio = doc.getElementsByTagName("bio")[0].getElementsByTagName("summary")[0].childNodes[1].nodeValue;

     bios[i] = bio; // store it in the array

     document.write("<div><button type='button' onclick='document.getElementById("+i+").innerHTML=bios["+i+"];'>Open Bio</button></div>");
}

Of course, while that will work it's a bunch of bad practises, including

  • unsecured accessing of DOM nodes/properties. If the xml changes its format, you will get lots of exceptions here. You might be sure now that this never happens, but wrapping artist and bio in try-catch might not be a bad idea.
  • snychronous Ajax. One can do better than that.
  • loading 20 documents (and that sequentially!) even if you don't need them. It might be worth to try loading each of them only when the respective button is clicked.
  • document.write
  • Inline attribute event handlers
  • …and creating them even by JS.
Bergi
  • 630,263
  • 148
  • 957
  • 1,375