/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Array references problem

Is there a way to reference different arrays from one function call?

I have 8 arrays of student names initialized as
SectA1 = new Array();
SectA1[0] = ‘Section A1’;
SectA1[1] = ‘Boop, Betty’
SectA1[2] = ‘Fudd, Elmer’;
SectA1[3] = ‘Oop, Alley’;

SectA2 = new Array();
SectA2[0] = ‘Section A2’;
SectA2[1] = ‘Bunny, Bugs’
SectA2[2] = ‘Duck, Daffy’;
SectA2[3] = ‘Mouse, Mickey’;

and 6 more arrays set with other names as follows in the <HEAD> section:
SectB1 = new Array();
SectB2 = new Array();
SectC1 = new Array();
SectC2 = new Array();
SectD1 = new Array();
SectD2 = new Array();
with different names initialized within the arrays.

This is what I also put in the <HEAD> section, but does not work:

[code=php]
function ShowSetup(Sect) {
for (i=0; i<Sect.length; i++) { tbl += Sect[i]+'<br />’; }
}
function SectionSetup(Section) {
tbl = ‘<pre>’;
if (Section == ‘a1’) { tbl += ShowSetup(SectA1); }
if (Section == ‘b1’) { tbl += ShowSetup(SectB1); }
if (Section == ‘c1’) { tbl += ShowSetup(SectC1); }
if (Section == ‘d1’) { tbl += ShowSetup(SectD1); }
if (Section == ‘a2’) { tbl += ShowSetup(SectA2); }
if (Section == ‘b2’) { tbl += ShowSetup(SectB2); }
if (Section == ‘c2’) { tbl += ShowSetup(SectC2); }
if (Section == ‘d2’) { tbl += ShowSetup(SectD2); }
tbl += ‘</pre>’;
return tbl;
}

function SetInfo(Info,Section) {
document.getElementById(Info).innerHTML = SectionSetup(Section);
}
[/code]

In the <BODY> I have:

[code=php]
<a href=”#” onClick=SetInfo(‘grp’,’a1′)>A1</a>
<a href=”#” onClick=SetInfo(‘grp’,’b1′)>B1</a>
<a href=”#” onClick=SetInfo(‘grp’,’c1′)>C1</a>
<a href=”#” onClick=SetInfo(‘grp’,’d1′)>D1</a>
<a href=”#” onClick=SetInfo(‘grp’,’a2′)>A2</a>
<a href=”#” onClick=SetInfo(‘grp’,’b2′)>B2</a>
<a href=”#” onClick=SetInfo(‘grp’,’c2′)>C2</a>
<a href=”#” onClick=SetInfo(‘grp’,’d2′)>D2</a>
<div id=”grp”> </div>
[/code]

My intent is to display a different set of names within the ‘grp’ <DIV> section
by passing a reference to the different array names.

I only get ‘undefined’ displayed in the <div> area.

So… is there a way to call the ShowSetup(Sect) function when the ‘Sect’ parameter is a different defined array?

I am trying to avoid using an eval() function to solve this problem.

What I want to do should be easy for someone, but not for me.

Any thoughts are welcome. ?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@aprilaprilAug 16.2007 — [CODE]
var tbl;
function ShowSetup(Sect) {
for (i=0; i<Sect.length; i++) {
tbl += Sect[i]+'<br />';
}
return tbl;
}

[/CODE]
Copy linkTweet thisAlerts:
@JMRKERauthorAug 16.2007 — Thanks 'aprilapril'.


Good eyes!!!

Stupid mistakes always get the better of me.

Here is the final version that works for me should anyone else have a need for such code.
[code=php]
<html>
<head>
<title>Multiple Array Referrences</title>

<script type="text/javascript">

SectA1 = new Array('Section A1','Boop, Betty','Fudd, Elmer','Oop, Alley');
SectA2 = new Array('Section A2','Bunny, Bugs','Duck, Daffy','Mouse, Mickey');
SectB1 = new Array('Section B1','Alley, Gasoline','Cat, Garfield','Dog, Odie');
SectB2 = new Array('Section B2','Dog, Under','Sam,Yosemiti','Starr, Brenda');
SectC1 = new Array('Section C1','Bear, Fozzy','Frog, Kermit','Gonzo, Great');
SectC2 = new Array('Section C2','Bear, Yogi','Dog, Deputy','Piggy, Miss');
SectD1 = new Array('Section D1','Bailey, Beetle','Cat, Felix','Oyl, Olive');
SectD2 = new Array('Section D2','Bumstead, Blondie','Bumstead, Cookie','Bumstead, Dagwood');

function ShowArray(Sect) {
var tbl = '';
for (i=0; i<Sect.length; i++) { tbl += Sect[i]+'<br />'; }
return tbl;
}

function SetSectionArray(Info,SectionArray) {
document.getElementById(Info).innerHTML = ShowArray(SectionArray);
}

</script>
</head>
<body>

<a href="#" onClick=SetSectionArray('grp',SectA1)>A1</a>
<a href="#" onClick=SetSectionArray('grp',SectB1)>B1</a>
<a href="#" onClick=SetSectionArray('grp',SectC1)>C1</a>
<a href="#" onClick=SetSectionArray('grp',SectD1)>D1</a>
<a href="#" onClick=SetSectionArray('grp',SectA2)>A2</a>
<a href="#" onClick=SetSectionArray('grp',SectB2)>B2</a>
<a href="#" onClick=SetSectionArray('grp',SectC2)>C2</a>
<a href="#" onClick=SetSectionArray('grp',SectD2)>D2</a>
<p />
<div id="grp"></div>

</body>
</html>
[/code]
×

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,
)...