/    Sign up×
Community /Pin to ProfileBookmark

Looping through fill options

Hi everyone,

I’m very new to javascript and I’m working on a little project. Anyhow I can’t quite figure out if I can or how to loop this code to cut down on its size. Basically I have 16 select form objects on my html page all named length1, length2, length3 etc all the way to 16. When the body loads I have an onload fillCategory(). That function fills the selectbox with options. Anyhow I would I loop this so I don’t have to have 200 lines of code for each select box.

Here is my current code with a few of the options. Additionally, I can’t just name them all length1 or something because each 1 is used differently to add time.

Thanks in advance,

[CODE]function addOption(selectbox, value, text )
{
var optn = document.createElement(“OPTION”);
optn.text = text;
optn.value = value;

selectbox.options.add(optn);
}

function fillCategory(){
// this function is used to fill the category list on load

addOption(document.time.length1, “0”, “0”, “”);
addOption(document.time.length1, “5”, “5”, “”);
addOption(document.time.length1, “6”, “6”, “”);
addOption(document.time.length1, “7”, “7”, “”);
addOption(document.time.length1, “8”, “8”, “”);
addOption(document.time.length1, “9”, “9”, “”);
addOption(document.time.length1, “10”, “10”, “”);
addOption(document.time.length1, “11”, “11”, “”);
addOption(document.time.length1, “12”, “12”, “”);
addOption(document.time.length1, “13”, “13”, “”);
addOption(document.time.length1, “14”, “14”, “”);
addOption(document.time.length1, “15”, “15”, “”);
addOption(document.time.length1, “20”, “20”, “”);
addOption(document.time.length1, “25”, “25”, “”);
addOption(document.time.length1, “30”, “30”, “”);

addOption(document.time.length2, “0”, “0”, “”);
addOption(document.time.length2, “5”, “5”, “”);
addOption(document.time.length2, “6”, “6”, “”);
addOption(document.time.length2, “7”, “7”, “”);
addOption(document.time.length2, “8”, “8”, “”);
addOption(document.time.length2, “9”, “9”, “”);
addOption(document.time.length2, “10”, “10”, “”);
addOption(document.time.length2, “11”, “11”, “”);
addOption(document.time.length2, “12”, “12”, “”);
addOption(document.time.length2, “13”, “13”, “”);
addOption(document.time.length2, “14”, “14”, “”);
addOption(document.time.length2, “15”, “15”, “”);
addOption(document.time.length2, “20”, “20”, “”);
addOption(document.time.length2, “25”, “25”, “”);
addOption(document.time.length2, “30”, “30”, “”);

addOption(document.time.length3, “0”, “0”, “”);
addOption(document.time.length3, “5”, “5”, “”);
addOption(document.time.length3, “6”, “6”, “”);
addOption(document.time.length3, “7”, “7”, “”);
addOption(document.time.length3, “8”, “8”, “”);
addOption(document.time.length3, “9”, “9”, “”);
addOption(document.time.length3, “10”, “10”, “”);
addOption(document.time.length3, “11”, “11”, “”);
addOption(document.time.length3, “12”, “12”, “”);
addOption(document.time.length3, “13”, “13”, “”);
addOption(document.time.length3, “14”, “14”, “”);
addOption(document.time.length3, “15”, “15”, “”);
addOption(document.time.length3, “20”, “20”, “”);
addOption(document.time.length3, “25”, “25”, “”);
addOption(document.time.length3, “30”, “30”, “”);

}[/CODE]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@joshpauthorApr 30.2009 — Well for anyone interested I figured it out. Can't believe I did but I did lol.

[CODE]
function fillCategory(){
// this function is used to fill the category list on load

var number = [0,5,6,7,8,9,10,11,12,13,14,15,20,25,30];
var lengths = [document.time.length1,document.time.length2,document.time.length3];
var blank = "";
var add = addOption;


for (i=0;i<number.length;i++){

for (t=0;t<lengths.length;t++){
add((lengths[t]),(number[i]),(number[i]),blank);
}
}
}
[/CODE]
×

Success!

Help @joshp 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,
)...