/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Passing array as function parameter in FF and MSIE

Can someone explain why this code works in FF, but fails in MSIE?
In particular the content / display of the <select> options.

What is different in the executions of the same script on different browsers?

What can I do or change to make it work in both browsers?

[code]
<html>
<head>
<title> Pass array by reference </title>
<script type=”text/javascript”>

var ClassOf2010 = [
‘John Jacob Jingleheimer Smith’,
‘Alice Wonderland’,
// … etc …
‘Seymore Butts’
];
var ClassOf2011 = [
‘Gasoline Alley’,
‘Betty Boop’,
‘Clarabel Cow’,
‘Deputy Dog’,
// … etc …
‘Elmer Fudd’
];

function showArray(List) { return List.join(‘n’); }

function $_(IDS) { return document.getElementById(IDS); }

function AddOptionItem(IDS,Item) {
Item.trim();
var sel = $_(IDS);
var tmp = Item.split(‘:’);
sel.options[sel.options.length]=new Option(tmp[0],Item,true,true);
}

function initStudentSBox(IDS,List) {
$_(IDS).options.length = 0;
$_(IDS).options[0]=new Option(‘Choose’,’:blank’,true,true);
for (var i=0; i<List.length; i++) { AddOptionItem(IDS,List[i]+’:’+i); }
$_(IDS).selectedIndex = 0;
}

onload = function() {
initStudentSBox(‘Studs2010’,ClassOf2010);
initStudentSBox(‘Studs2011’,ClassOf2011);
}

</script>
</head>
<body>
<button onclick=”alert(showArray(ClassOf2010))”>Show Class of 2010</button>
<select id=”Studs2010″ onchange=”alert(this.value)”></select>
<p>
<button onclick=”alert(showArray(ClassOf2011))”>Show Class of 2011</button>
<select id=”Studs2011″ onchange=”alert(this.value)”></select>
</body>
</html>
[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@Logic_AliDec 08.2010 — Even the I.E. error console makes it pretty clear: [I]String.trim[/I] is undefined.

Just add this:
if( String().trim === undefined )
String.prototype.trim = function()
{
return this.replace( /^s+|s+$/g, "" );
}
Copy linkTweet thisAlerts:
@JMRKERauthorDec 08.2010 — Thank you. ?

I got so caught up into thinking it was a browser problem,

I forgot to check!

I was looking at <!doc ... > tags and everywhere but the obvious.
×

Success!

Help @JMRKER 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 4.29,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...