2

I'm trying to update a set of divs class="oct_days" to give them id based on :nth-child(n). The format of the id is oct_n. I'm trying to accomplish this using a for loop to set this for divs.

window.onload = function addOctDate() {
    var cls = document.getElementByClass("oct_days");
    for (var n = 1; n < 32; n++) {
        cls[n].id = "oct_" + n;
    }
};

Fiddle (http://jsfiddle.net/ascottz/D9Exm/)

The idea is to have .oct_days:nth-child(1) have id="oct_1", but id isn't being set.

1
  • 1
    If you intend to set an attribute, it's better to do it explicitly with setAttribute. This isn't necessary for id, but means your code will be more uniform across DOM manipulations Commented Oct 9, 2013 at 1:39

4 Answers 4

3

clsyour issues are this:

  1. window.onload was being run before your html was initialized
  2. you need to call document.getElementsByClassName not
  3. you are starting your iteration at 1, indexes are 0 based and you should start there and add the + 1 as noted below
  4. also, while iterating, its good to only iterate only over the known items in your list

try this code:

function addOctDate() {
        var cls = document.getElementsByClassName("oct_days"); 
        for (n=0, length = cls.length; n < length; n++) {
            cls[n].id= "oct_" + (n + 1); 
        }
    }; 

addOctDate()
Sign up to request clarification or add additional context in comments.

Comments

2

The function is getElementsByClassName.

The fiddle doesn't work because you're seeing window.onload while your code is already being run inside that event (the dropdown on the left says onLoad). It'll also error out because you don't have 31 elements in the HTML, but it'll still set the IDs.

Comments

1

Your code is very simple to fix

(function () {

  // .getElementsByClassName not .getElementByClass
  var cls = document.getElementByClassName("oct_days"),
  // set the stopping point DYNAMICALLY
      len = cls.length,
  // start the index at 0;
      n = 0;
  for (; n < len; n++) {
    cls[n].id = "oct_" + (n + 1);
  }
// ()(); auto runs the function 
})();

Comments

0

Here is a way to add ids to elements and classes using just plain js.

HTML

<div id="test">
    Content will append below!
    <input type="button" value="click me!" onClick="myFunction();"/>
</div>

CSS

.cool_0 {
    background: red;
    height: 200px;
    width: 100%;
}
.cool_1 {
    background: yellow;
    height: 200px;
    width: 100%;
}
.cool_2 {
    background: red;
    height: 200px;
    width: 100%;
}
.cool_3 {
    background: yellow;
    height: 200px;
    width: 100%;
}
.cool_4 {
    background: red;
    height: 200px;
    width: 100%;
}
.cool_5 {
    background: yellow;
    height: 200px;
    width: 100%;
}

JS

function myFunction(){
        var myId = 0;
        var counter = 0;
        var myDiv = document.getElementById("test")
        for(var i = 0; i < 5; i++){
            var textNode = document.createTextNode("sup! My current id is "+myId+" !")
            var t = document.createElement("div");
            t.setAttribute("id", counter++)
            t.setAttribute("class", "cool_"+myId++)
            t.appendChild(textNode)
            myDiv.appendChild(t);
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.