...

View Full Version : make javascript code more dynamic



crmpicco
10-14-2005, 04:12 PM
else if (segments == 6)
{
if ((document.forms["form"].elements["ad1"].value == "") && (document.forms["form"].elements["ad2"].value != "") && (document.forms["form"].elements["ad3"].value != "") && (document.forms["form"].elements["ad4"].value != "") && (document.forms["form"].elements["ad5"].value != "") && (document.forms["form"].elements["ad6"].value == ""))
{
el.innerHTML = el.innerHTML + seatArr[s-2];
}
if ((document.forms["form"].elements["ad1"].value == "") && (document.forms["form"].elements["ad2"].value == "") && (document.forms["form"].elements["ad3"].value == "") && (document.forms["form"].elements["ad4"].value == "") && (document.forms["form"].elements["ad5"].value != "") && (document.forms["form"].elements["ad6"].value == ""))
{
el.innerHTML = el.innerHTML + seatArr[s-5];
}
if ((document.forms["form"].elements["ad1"].value == "") && (document.forms["form"].elements["ad2"].value == "") && (document.forms["form"].elements["ad3"].value == "") && (document.forms["form"].elements["ad4"].value != "") && (document.forms["form"].elements["ad5"].value == "") && (document.forms["form"].elements["ad6"].value == ""))
{
el.innerHTML = el.innerHTML + seatArr[s-4];
}
if ((document.forms["form"].elements["ad1"].value == "") && (document.forms["form"].elements["ad2"].value != "") && (document.forms["form"].elements["ad3"].value == "") && (document.forms["form"].elements["ad4"].value == "") && (document.forms["form"].elements["ad5"].value == "") && (document.forms["form"].elements["ad6"].value == ""))
{
el.innerHTML = el.innerHTML + seatArr[s-2];
}
if ((document.forms["form"].elements["ad1"].value == "") && (document.forms["form"].elements["ad2"].value == "") && (document.forms["form"].elements["ad3"].value != "") && (document.forms["form"].elements["ad4"].value == "") && (document.forms["form"].elements["ad5"].value == "") && (document.forms["form"].elements["ad6"].value == ""))
{
el.innerHTML = el.innerHTML + seatArr[s-3];
}
if ((document.forms["form"].elements["ad1"].value == "") && (document.forms["form"].elements["ad2"].value == "") && (document.forms["form"].elements["ad3"].value == "") && (document.forms["form"].elements["ad4"].value == "") && (document.forms["form"].elements["ad5"].value == "") && (document.forms["form"].elements["ad6"].value != ""))
{
el.innerHTML = el.innerHTML + seatArr[s-6];
}
if ((document.forms["form"].elements["ad1"].value == "") && (document.forms["form"].elements["ad2"].value != "") && (document.forms["form"].elements["ad3"].value == "") && (document.forms["form"].elements["ad4"].value != "") && (document.forms["form"].elements["ad5"].value == "") && (document.forms["form"].elements["ad6"].value != ""))
{
el.innerHTML = el.innerHTML + seatArr[s-2];
}



I have written this JavaScript code which is just one condition in the function. Can anyone see a way of making it more dynamic and cutting down the code?

I cant see it,

:-(

Picco

Bill Posters
10-14-2005, 04:42 PM
Firstly, you should look at committing those objects to simpler variable names to save on the lengthy repetitions.

e.g.

var ad1Obj = document.forms["form"].elements["ad1"];
Subsequent instances of document.forms["form"].elements["ad2"] can be switched for ad2Obj (and so on for the other adx objects.

Instances of this code structure…

el.innerHTML = el.innerHTML + … ;
…can be replaced with…

el.innerHTML += … ;


This should make the code a little lighter, but I'm a little too rusty with js to know if more can be done to make the body of if statements any more efficient than that.

I'd look into switch statements, though I'm not sure they can be applied to such a complex array of possibilities. :rusty: ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum