/    Sign up×
Community /Pin to ProfileBookmark

Hello,

I’m trying to compare an array to a variable and see if it has the same value then output the second part of the array:

[CODE]
<script language=”javascript”>

var photoCaptionID0 = “13006”;
var photoCaptionID1 = “24018”;
var photoCaptionID2 = “13002”;
var photoCaptionID3 = “24065”;
var photoCaptionID4 = “24021”;
var photoCaptionID5 = “24005”;

var captionID = new Array();
captionID[0] = “13006, Item 1”;
captionID[1] = “24018, Item 2”;
captionID[2] = “13002, Item 3”;
captionID[3] = “24065, Item 4”;
captionID[4] = “24021, Item 5”;
captionID[5] = “24005, Item 6”;

for(var i = 0; i < captionID.length; i++){
if (captionID[i][0] == photoCaptionID0){
break;
document.getElementById(“photoCaptionID0”).innerHTML = captionID[i][1];

}
}

</script>

So using this example. If any of the first section in the array captionID (24001, 13001, etc) has the same value as photoCaptionID0, then print the second part of captionID (Item 1, Item 2, etc).
[/CODE]

Thanks much!

–thesprucegoose

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@A1ien51Mar 16.2009 — Learn about Objects:

<i>
</i>var photoCaptionID0 = "13006";
var photoCaptionID1 = "24018";
var photoCaptionID2 = "13002";
var photoCaptionID3 = "24065";
var photoCaptionID4 = "24021";
var photoCaptionID5 = "24005";

var captionID = {
"13006" : "Item 1",
"24018" : "Item 2",
"13002" : "Item 3",
"24065" : "Item 4",
"24021" : "Item 5",
"24005" : "Item 6"
}


alert(captionID[photoCaptionID0]);


Eric
×

Success!

Help @thesprucegoose 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.18,
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,
)...