Array issue

S

Sky

Hi Guru,

I need some help with Array in Javascript in populating the list to
SELECT box dynamically

I have a form that contains:
- 3 HTML checkboxes for region (Europe, America, Asia)
- 1 HTML Select box for language that dynamically populated based
on the region selected (ListA)

Europe : English, French, German, Spanish, Italian
America: English, Spanish, Portuguese
Asia: English, Chinese, Japanese, Korean

Example:
If Europe and America Region are selected, then the Select box for
language will have following (with no repeat of same language):
English, French, German, Spanish, Italian, Portuguese


In my script, I declare:

var arr = new Array(); // temp arrary
var arr_EU = new
Array("English","French","German","Spanish","Italian");
var arr_AM = new Array("English","Spanish","Portuguese");
var arr_AS = new Array("English","Chinese","Japanese","Korean");

if (myform.Europe.checked==true)
{
// check current temp array, and compliment the temp array
with arr_EU
}

if (myform.Europe.checked==false)
{
// check current temp array, and remove the item that
(found exclusively in arr_EU, and not in other arr_AM/arr_AS, if
arr_AM/arr_AS was checked)
}

// do similar for "myform.America.checked" and
"myform.Asia.checked"

// clear existing items
myform.ListA.options.length = 0

// loops through the array adding item text/value
for(var i=0;i<arr.length;i++)
{
document.forms[0].ListA.options.length ++;
document.forms[0].ListA.options.value = arr.split(",")[0];
document.forms[0].ListA.options.text = arr.split(",")[1];
}


Please help. Many Thanks.

Regards,
Sky
 
J

Jukka K. Korpela

Sky said:
I need some help with Array in Javascript in populating the list to
SELECT box dynamically

This looks like an assignment in a programming course, and not a
particularly good one, and you haven't really tried to solve it (no URL),
mainly just wrote some comments on what would be done and some fragments of
code, with syntax errors. Moreover, the "Array issue", whatever it might be,
is a small part of your problems.
I have a form that contains:
- 3 HTML checkboxes for region (Europe, America, Asia)
- 1 HTML Select box for language that dynamically populated based
on the region selected (ListA)

The very idea is questionable. Why should region selection dictate the
options for language selection?
Europe : English, French, German, Spanish, Italian
America: English, Spanish, Portuguese
Asia: English, Chinese, Japanese, Korean

People speak Portuguese in Europe, too (there's Portugal, you know), and in
Asia, and so on. Region and language selections should simply be two
independent selections. The only reason to make one depend on the other is
that you have some content or service that imposes restrictions, e.g. so
that the company's offers that are valid for Europe are not available in
Portuguese, etc. Even in such a case, using JavaScript (alone) is
questionable. Why throw away customers who have JavaScript disabled or
filtered out? You should at least have normal links as a noscript backup;
and you might just as well start with it.
In my script, I declare:

You haven't posted the HTML markup, and posting JavaScript on Usenet tends
to break things, like lines (e.g., a long comment line becomes split, and
then part of the comment becomes code).
if (myform.Europe.checked==true)

Radio buttons don't work that way. Besides, if this is just inside your
script code that gets executed unconditionally, then you could at best test
for the _initial_ setting of radio buttons. To make things change
dynamically by user actions, you need event handlers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top