1

As the title says, I'm trying to change the ID of all elements with the tag "div". Here's my current script, which does not work:

function setIDs() {
    var divs[] = new Array();
    for(i in document.getElementsByTagName('div'))
        divs[i] = document.getElementsByTagName('div')[i];
    for(idNum in divs) divs[idNum].id="child"+idNum;
}

So after setIDs() is finished, my HTML should look like this:

<div id="child0">...</div>
<div id="child1">...</div>
...
<div id="childn">...</div>
RandomDuck.NET
  • 490
  • 5
  • 17

4 Answers4

2

Don't use for...in and reuse document.getElementsByTagName result.

function setIDs() {
  var divs = document.getElementsByTagName('div');
  for(var i=0; i<divs.length; i++) {
    divs[i].id = "child" + i;
  }
}
Community
  • 1
  • 1
Alexander
  • 23,432
  • 11
  • 63
  • 73
0

Invalid JavaScript.

 var divs[] = new Array();

Also, do not use for-in for Array or Array-like iteration. It will often fail.

  • Pretty generalized on the whole 'it will often fail' answer. Could you please elaborate? if I'm iterating over the keys and indexes of an element then I cannot see any reason why `for...in` is not an acceptable method. – Ohgodwhy Jun 19 '12 at 22:02
  • @Ohgodwhy: `for-in` is for general property enumeration, irrespective of the property and its location in the prototype chain. All enumerable properties will be included in the loop. Running [this example](http://jsfiddle.net/qRuMp/) in Firefox, you'll see more than just `div` elements in the console. Also it does not guarantee a sequential numeric order. Yes, my statement is generalized, and is meant to be. If index iteration is needed, `for-in` is very simply the wrong tool in JavaScript. –  Jun 20 '12 at 01:40
0

Can you use jQuery? If so, you should try something like this:

var x = 0;
$("div").each(function(){
    $(this).attr("id", "child"+x);
    x++;
});
Simon
  • 3,580
  • 2
  • 23
  • 24
  • that's not even how it should be with jQuery. – Bergi Jun 19 '12 at 21:42
  • Working yes, but awful. Should be `$("div").attr(function(i){return "child"+i;});` – Bergi Jun 19 '12 at 21:52
  • Sorry Bergi, your code doesn't work . And the question was tagged 'jQuery', that's why I allowed myself to answer like this. Anyway I'm not here to fight, so sorry if my answer bothered you. – Simon Jun 19 '12 at 21:56
  • oops, I missed the obvious: `$("div").attr("id", function(i){return "child"+i;})` – Bergi Jun 19 '12 at 22:41
  • I don't see why this should have a down-vote (although using the index is nicer than an `x++`) so ... +1 –  Jun 20 '12 at 04:02
-1
function setIDs() {
    var divs[] = new Array();
    // no need for an extra array. Invalid identifier, btw.

    for(i in document.getElementsByTagName('div'))
    // never run through Arrays with for-in-loops. It's the same for NodeLists.

        divs[i] = document.getElementsByTagName('div')[i];
        // you might have cached the NodeList to a variable

    for(idNum in divs) divs[idNum].id="child"+idNum;
    // again, don't use a for-in-loop. And why at all do you need a second loop?
}

corrected:

function setIDs() {
    var divs = document.getElementsByTagName('div');
    for (var i=0; i<divs.length; i++) {
        divs[i].id = "child"+i;
    }
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375