views:

147

answers:

2

I'm using the following Javascript code to populate a DIV with an ordered list:

// send script back in split list
var scriptList = script.split("\n");
var finalScript = "<ol>\n";
var count = 0;
while(scriptList.length >= count) {
    if((scriptList[count]=="") || (scriptList[count] == undefined))  {
        count ++;
        continue;
    }

    finalScript = finalScript + "<li>" + scriptList[count] + "</li>\n";
    count ++;
}
finalScript = finalScript + "</ol>";
scriptingDiv.innerHTML = finalScript;

In firefox, if i look in the DOM using Firebug, this correctly translates to the following and correctly displays an ordered list.

<ol>
<li>This is the first item in the list</li>
<li>This is the second item in the list</li>
</ol>

In IE, it displays as if the </li> tags are <br /> tags and ignores all the other tags, like this:

This is the first item in the list
This is the second item in the list

Do I need to dynamically add the ordered list to the DOM for this to work? As opposed to just setting the html code in the div using .innerHTML?

TIA

+1  A: 

Do I need to dynamically add the ordered list to the DOM for this to work? As opposed to just setting the html code in the div using .innerHTML?

Yes

var scriptList = script.split("\n");
var count = 0;
var ol = document.createElement("ol");

    for(var index=0; index<scriptList.length; index++) {
        if(scriptList[index]!="")  {
            var li = document.createElement("li");
            li.innerHTML=scriptList[index];
            ol.appendChild(li);
        }
    }

scriptingDiv.appendChild(ol);
Gregoire
+1 (and nearly -1 for sticking with that horrible abuse of the `while` loop…)
Tomalak
@Tomalak: I was too lazy to change this..
Gregoire
It's still behaving the same way in IE7. :( IE8 works correctly.. Any ideas? We have to support IE7 as that's what most of our users use.
Matt
@Matt: I have tested this script in IE6 (I do not have IE7 right now) and it is working, so your problem must come from somewhere else
Gregoire
@gregoire That it is. Thank you both for your help.
Matt
A: 

Why not use dom methods instead? IE:

myOL = document.createElement("ol");
myLI = document.createElement("li");
myTxt = document.createElement("My Text!");
myLI.appendChild(myTxt);
myOL.appendChild(myLI);

etc

edl