0

I have tried to generate dynamic Control. It is working on FF but IE gives error:

divcntrl[i] is undefined.

Please check the following code:

 var mc=document.getElementById("maindiv");
    var divcntrl = new Array();
    for(var i=0; i<10;i++) {
      divcntrl[i]=document.createElement("div");\\error here
    divcntrl[i].setAttribute("id","div"+ i);
      mc.appendChild(divcntrl[i]);
}
1

3 Answers 3

1

There is no such thing as <div1> element. Running your code in a fiddle, it runs without errors so the problem is somewhere else. Are you cached with a bad version of the file?

var mc=document.getElementById("maindiv");
var divcntrl = new Array();
for(var i=0; i<10;i++) {
    divcntrl[i]=document.createElement("div");
    divcntrl[i].innerHTML = i;
    mc.appendChild(divcntrl[i]);
}​

I think you would want to code it more like

var mc = document.getElementById("maindiv");
var divcntrl = [];
for (var i=0; i<10; i++) {
    var div = document.createElement("div");
    div.id = "div" + i;
    div.innerHTML = i;
    divcntrl.push(div);
    mc.appendChild(div);
}​
Sign up to request clarification or add additional context in comments.

1 Comment

Your problem is something else since that code will run and it has been proven to run. Clean your cache, make sure you have all the correct files loaded.
0

You can create a DIV element, NOT a "div"+i element, since "div"+i is not part of any HTML specification ("div1" is not an HTML element). Are you confusing "div" and "id"?

Comments

0

The problem is with this line

divcntrl[i]=document.createElement("div" + i);

Just try this:

var divElt = document.createElement("div");
divElt.id = i;
divcntrl[i] = divElt;

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.