PDA

View Full Version : Is there an elegant way to do this?


dominicosavio
01-23-2003, 02:19 PM
I have this function:


function populateValue(value){
if (document.layers){
if (value == "nonmember"){
document.layers.div1.document.write("a");
document.layers.div2.document.write("b");
document.layers.div3.document.write("c");
document.layers.div4.document.write("d");
.....
.....
document.layers.div24.document.write("z");

} else if (value == "member"){
document.layers.div1.document.write("1");
document.layers.div2.document.write("2");
document.layers.div3.document.write("3");
document.layers.div4.document.write("4");
....
....
document.layers.div24.document.write("24");
} else if (value == "staff"){
document.layers.div1.document.write("a1");
document.layers.div2.document.write("b2");
document.layers.div3.document.write("c3");
document.layers.div4.document.write("d4");
....
....
document.layers.div24.document.write("z24");
}
} else if (document.all) {
if (value == "nonmember"){
div1.innerHTML="a";
div2.innerHTML="b";
div3.innerHTML="c";
div4.innerHTML="d";
....
....
div24.innerHTML="z";
} else if (value == "member"){
div1.innerHTML="1";
div2.innerHTML="2";
div3.innerHTML="3";
div4.innerHTML="4";
....
....
div24.innerHTML="24";
} else if (value == "staff"){
div1.innerHTML="a1";
div2.innerHTML="b2";
div3.innerHTML="c3";
div4.innerHTML="d4";
....
....
div24.innerHTML="z24";
}
}
}


Can someone rewrite this to make this more elegant. Besides, can we put the value "a, b, c, d......" and "1, 2, 3, 4, ..." into an array so we can expand the value, and div as many as you want,

Hope you guys understand what I am talking about.

beetle
01-23-2003, 03:28 PM
I do believe this should do it (I haven't tested, so please be detailed if it throws errors)function populateValues( value )
{
var alphabet = ['','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x',' y','z'];
for ( var i = 1; i <= 26; i++ )
{
switch ( value )
{
case 'nonmember' : populateObject( i, alphabet[i] ); break;
case 'member' : populateObject( i, i ); break;
case 'staff' : populateObject( i, alphabet[i] + i ); break;
}
}
}

function populateObject( index, value )
{
if ( document.layers )
document.layers['div' + index].document.write( value );
else
( document.all || document.getElementById )( 'div' + index ).innerHTML = value;
}

dominicosavio
01-23-2003, 03:55 PM
Thank you. I will try it now, and will come back to let you know the result.

dominicosavio
01-23-2003, 03:59 PM
Perfect. Works like a charm.

Thank you again, and again for your quick reply.

beetle
01-23-2003, 04:33 PM
Originally posted by dominicosavio
Perfect. Works like a charm.

Thank you again, and again for your quick reply. Wehee! I'm getting better at this "coding without debugging/testing" thing.

Glad to help :D

whammy
01-23-2003, 11:55 PM
Don't you love it when you do that?

I wrote a couple of scripts for a fellow developer a couple of months ago, and I was totally amazed when they worked perfectly the first time, as I hadn't tested them at all. ;)

Seems I can never do that when I'm writing them for myself. :D

beetle
01-23-2003, 11:56 PM
Originally posted by whammy
Seems I can never do that when I'm writing them for myself. :D Boy isn't THAT the truth. ;)