By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,021 Members | 952 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,021 IT Pros & Developers. It's quick & easy.

Select an Array, to Display

P: n/a
It is a familiar story. It works in Firefox, but not in IE.

I want to dynamically populate a select box with an array of values
based upon the value selected from another select. The arrays are
defined when the page is loaded.

Assuming that the arrays are name a, b. and c:

<select name="someArray">
<option onclick="selArr(a);" value="a">A</option>
<option onclick="selArr(b);" value="b">B</option>
<option onclick="selArr(c);" value="c">C</option>
</select>

function selArr(whichArr) {
var optStr;
var cnt = whichArr.length;
for (var i=0; i<cnt; i++) {
optStr += '<option value="'+whichArr[i].k+'
'+whichArr[i].v+'">'+whichArr[i].k+' '+whichArr[i].v+'</option>\n';
}
document.getElementById('sel2').innerHTML = optStr;
}

I have run into this before where IE ignores any calls to a function
from an option value. However, the problem is if I call the function
from the select tag:
<select name="someArray" onchange="selArr[this.value);">
this.value is treated not as the defined array but as a var value, as
if is enclosed by quotes.

Any ideas how to get around this?

TIA!
Jan 31 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Tyrone Slothrop wrote:
It is a familiar story. It works in Firefox, but not in IE.

I want to dynamically populate a select box with an array of values
based upon the value selected from another select. The arrays are
defined when the page is loaded.

Assuming that the arrays are name a, b. and c:

<select name="someArray">
<option onclick="selArr(a);" value="a">A</option>
<option onclick="selArr(b);" value="b">B</option>
<option onclick="selArr(c);" value="c">C</option>
</select>

function selArr(whichArr) {
var optStr;
var cnt = whichArr.length;
for (var i=0; i<cnt; i++) {

function selArr(whichArr){
var cnt = whichArr.length,
sel=document.forms[0].sel2.options,sel.length=0,i;
for(i=0;i<cnt;i++){
sel[i]=new Option(whichArr[i],whichArr[i])
}
}
select name="someArray" onchge="selArr[this.value);"> this.value is treated not as the defined array but as a var value, as
if is enclosed by quotes.

Any ideas how to get around this?


onchange="selArr(window[this[this.selectedindex].value]);
// provided the arrays are global in scope

[...]
Mick
Jan 31 '06 #2

P: n/a
On Tue, 31 Jan 2006 18:38:52 GMT, mick white <mi**@mickweb.com> wrote:
Tyrone Slothrop wrote:
It is a familiar story. It works in Firefox, but not in IE.

I want to dynamically populate a select box with an array of values
based upon the value selected from another select. The arrays are
defined when the page is loaded.

Assuming that the arrays are name a, b. and c:

<select name="someArray">
<option onclick="selArr(a);" value="a">A</option>
<option onclick="selArr(b);" value="b">B</option>
<option onclick="selArr(c);" value="c">C</option>
</select>

function selArr(whichArr) {
var optStr;
var cnt = whichArr.length;
for (var i=0; i<cnt; i++) {

function selArr(whichArr){
var cnt = whichArr.length,
sel=document.forms[0].sel2.options,sel.length=0,i;
for(i=0;i<cnt;i++){
sel[i]=new Option(whichArr[i],whichArr[i])
}
}
select name="someArray" onchge="selArr[this.value);">

this.value is treated not as the defined array but as a var value, as
if is enclosed by quotes.

Any ideas how to get around this?


onchange="selArr(window[this[this.selectedindex].value]);
// provided the arrays are global in scope

[...]
Mick


Thanks Mick!

I figured out how to do a lot of this on my own, though the onChange
part looks like what I need to make it completely dynamic.

I have been coding in PHP, C and Perl for years but JavaScript still
makes me nuts at times. ;-)

Thanks again!

Jan 31 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.