/    Sign up×
Community /Pin to ProfileBookmark

Array of objects as a function parameter

Hi guys…

Seriously, I don’t get it. I’ve been searching for a while to get an answer to this. Hope this forum will help me.

[CODE]function drawMenuCategories (bodyHandle,nom0,nom1,nom2,nom3,nom4) {

var categorie0 = new Object();
categorie0.nom = nom0;
categorie0.presenceItems = false;

var categorie1 = new Object();
categorie1.nom = nom1;
categorie1.presenceItems = true;

var categorie2 = new Object();
categorie2.nom = nom2;
categorie2.presenceItems = true;

var categorie3 = new Object();
categorie3.nom = nom3;
categorie3.presenceItems = true;

var categorie4 = new Object();
categorie4.nom = nom4;
categorie4.presenceItems = true;

var catArray = new Array(categorie0,categorie1,categorie2,categorie3,categorie4);

nbCat = 5;

str = ‘<table cellspacing=”0″ id=”idTableCat” style=”border:’+menuBorder+’px solid #’+menuBorderColor+’; cursor:’+pointerTypeCat+’;” background=”images/top_menu_bg.jpg” onmouseout=”setTimeout(‘clearAll()’,3000);”><tr>’;

for (i=0; i<nbCat;i++) {

str += ‘<td style=”color:#’+catFontColor+'” id=”idCat’+i+'” onmouseover=”setTimeout(‘showItems(‘+i+’,’+nbCat+’,’+catArray+’)’,’+delayShowItems+’);”>’;

for (a=0; a<spacingCat; a++) {
str += ‘&nbsp;’;
}

str += catArray[i].nom;

for (a=0; a<spacingCat; a++) {
str += ‘&nbsp;’;
}

if (catArray[i].presenceItems == true) str += ‘<img src=”images/black_arrow.gif” />’;

}

str += ‘</tr></table>’;

bodyHandle.innerHTML = str; //on &#233;crit la table des cat&#233;gories
}[/CODE]

This function draws my javascript drop down menu categories and it works.

The line

[CODE]str += catArray[i].nom;[/CODE]

outputs correctly my categorie names.

However, I get an error message for passing my array catArray as a function parameter. So this line

[CODE]str += ‘<td style=”color:#’+catFontColor+'” id=”idCat’+i+'” onmouseover=”setTimeout(‘showItems(‘+i+’,’+nbCat+’,’+catArray+’)’,’+delayShowItems+’);”>’;[/CODE]

returns the following error with firefox console when I try to mouseover on a categorie using my browser :

Erreur&#160;: missing ] after element list
Fichier source&#160;: [url]http://74.57.73.48:8080/cie/[/url]
Ligne&#160;: 1, Colonne&#160;: 22
Code source&#160;:
showItems(2,5,[object Object],[object Object],[object Object],[object Object],[object Object])

Anyone has any idea. I’ve been searching the web for some explanation but no success so far.

Thx!

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@cgishackFeb 09.2008 — Well the array is not a string thats why it does not work.....

You need to set the onmouseover programmatically.

<i>
</i>element.onmouseover = function() { someFunction(a,b,c) }
Copy linkTweet thisAlerts:
@hudonyauthorFeb 09.2008 — But my first two parameters aren't string either, they are integer and they are processed correctly.

Am I missing something?

?
Copy linkTweet thisAlerts:
@cgishackFeb 09.2008 — Integer or String... they can be written to a page.

An object is made up of many different parts and unless serialized into a string (JSON) you have to set the event programmatically like in my earlier example.
Copy linkTweet thisAlerts:
@hudonyauthorFeb 09.2008 — Integer or String... they can be written to a page.

An object is made up of many different parts and unless serialized into a string (JSON) you have to set the event programmatically like in my earlier example.[/QUOTE]


Oh, I see...

Il try to do it the way you describe.

Thx.
Copy linkTweet thisAlerts:
@KorFeb 09.2008 — You may also use a closure, in case the variables a,b,c are dynamic or in-loop generated
<i>
</i>element.[COLOR="Blue"]A[/COLOR]=a;
element.onmouseover=function(){someFunction([B]this[/B].[COLOR="Blue"]A[/COLOR])}
×

Success!

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