/    Sign up×
Community /Pin to ProfileBookmark

What’s wrong with my script?

Hi,
I created a script that takes a list and separates it into two divs (yeah, I know–don’t ask me why I had to do this with js and not in the HTML… it’s a long story).

It works as it should in Firefox/Safari/Chrome, but fails in IE 6 and 7. I can’t for the life of me figure out why this should be.

Could you find it in your hearts to help a poor Javascript noobie?

[code]function start()
{
startList();
columnList(‘menulist_2tcc’);
columnList(‘menulist_3tcc’);
columnList(‘menulist_4tcc’);
}

function columnList(src)
{
var list_ul = document.getElementById(src);

var left_div = document.createElement(‘div’);
var right_div = document.createElement(‘div’);

var items = list_ul.childNodes;

if (src == ‘menulist_3tcc’)
{
var itemsLength = items.length/2-1;
}
else
{
var itemsLength = items.length/2;
}

for (i = 0; i < itemsLength; i++)
{
left_div.appendChild(items[0]);
}

itemsLength = items.length;

for (i = 0; i < itemsLength; i++)
{
right_div.appendChild(items[0]);
}

list_ul.appendChild(left_div);
list_ul.appendChild(right_div);

left_div.setAttribute(‘id’, ‘left’);
right_div.setAttribute(‘id’, ‘right’);
}

function startList()
{
if (document.all && document.getElementById)
{
navRoot = document.getElementById(“menulist_roottcc”);
for (i = 0; i < navRoot.childNodes.length; i++)
{
node = navRoot.childNodes[i];
if (node.nodeName == “LI”)
{
node.onmouseover = function()
{
this.className += ” over”;
}

node.onmouseout = function()
{
this.className = this.className.replace(” over”, “”);
}
}
}
}
}[/code]

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@PorpoiseMuffinsauthorFeb 08.2010 — I'm sorry, I really screwed that up. The important part of the Javascript is this:

[code]function columnList(src)
{
var list_ul = document.getElementById(src);

var left_div = document.createElement('div');
var right_div = document.createElement('div');

var items = list_ul.childNodes;

if (src == 'menulist_3tcc')
{
var itemsLength = items.length/2-1;
}
else
{
var itemsLength = items.length/2;
}

for (i = 0; i < itemsLength; i++)
{
left_div.appendChild(items[0]);
}

itemsLength = items.length;

for (i = 0; i < itemsLength; i++)
{
right_div.appendChild(items[0]);
}

list_ul.appendChild(left_div);
list_ul.appendChild(right_div);

left_div.setAttribute('id', 'left');
right_div.setAttribute('id', 'right');
}[/quote]

The part below that was just the suckerfish menu fix for IE6.
Copy linkTweet thisAlerts:
@FangFeb 08.2010 — Are you moving list items into a div?
Copy linkTweet thisAlerts:
@PorpoiseMuffinsauthorFeb 08.2010 — Are you moving list items into a div?[/QUOTE]

Yes. Well, first it divides the list into two parts of equal length and then puts each part in its own div (left & right).

Like I said, it works in everything but IE6/7. Is there some common Javascript bug in IE 7 and below that would cause this? I'm quite used to the horrors of working with CSS in IE, but I didn't think the same was true of Javascript...
Copy linkTweet thisAlerts:
@FangFeb 08.2010 — Where is the list container (ul/ol) in the div?
Copy linkTweet thisAlerts:
@PorpoiseMuffinsauthorFeb 08.2010 — Where is the list container (ul/ol) in the div?[/QUOTE]

I'm not sure what you mean. Do you mean after the javascript runs, or before? Before it runs, the divs don't exist.
Copy linkTweet thisAlerts:
@JMRKERFeb 09.2010 — Try this ... ?
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Move List to Div&lt;/title&gt;
&lt;script type="text/javascript"&gt;
// From: http://www.webdeveloper.com/forum/showthread.php?p=1066562#post1066562

function columnList() {
var sel = document.getElementById('ListInfo');
var full = parseInt(sel.options.length);
half = Math.floor(full/2);
// alert('Half : Fulln'+half+' : '+full);
var str = '';
for (var i=0; i&lt;half; i++) {
str += sel.options[i].value+'&lt;br&gt;';
}
document.getElementById('DivInfo1').innerHTML = str+'&lt;p&gt;';
str = '';
for (var i=half; i&lt;full; i++) {
str += sel.options[i].value+'&lt;br&gt;';
}
document.getElementById('DivInfo2').innerHTML = str;
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form onsubmit="return false"&gt;
&lt;select id="ListInfo"&gt;
&lt;option value="1"&gt;1&lt;/option&gt;
&lt;option value="2"&gt;2&lt;/option&gt;
&lt;option value="3"&gt;3&lt;/option&gt;
&lt;option value="4"&gt;4&lt;/option&gt;
&lt;option value="5"&gt;5&lt;/option&gt;
&lt;option value="6"&gt;6&lt;/option&gt;
&lt;option value="7"&gt;7&lt;/option&gt;
&lt;option value="8"&gt;8&lt;/option&gt;
&lt;option value="9"&gt;9&lt;/option&gt;
&lt;option value="10"&gt;10&lt;/option&gt;
&lt;option value="11"&gt;11&lt;/option&gt;
&lt;/select&gt;
&lt;button onclick="columnList()"&gt;Move to Columns&lt;/button&gt;
&lt;div id="DivInfo1" style="background-color:yellow;width:100px;"&gt;&lt;/div&gt;
&lt;div id="DivInfo2" style="background-color:cyan;width:100px;"&gt;&lt;/div&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

Close ??? :eek:
×

Success!

Help @PorpoiseMuffins spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...