...

View Full Version : Comparing Arrays



thesprucegoose
03-16-2009, 06:03 PM
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:



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

Thanks much!

--thesprucegoose

Philip M
03-16-2009, 06:22 PM
Try this:-


<div id = "photoCaptionID0"></div>

<script type= "text/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] = "13002, Item 1";
captionID[1] = "24018, Item 2";
captionID[2] = "13006, Item 3"; //match
captionID[3] = "24065, Item 4";
captionID[4] = "24021, Item 5";
captionID[5] = "24005, Item 6";

for (var i = 0; i < captionID.length; i++) {
var x = captionID[i].split(",");
if (x[0] == photoCaptionID0) {
document.getElementById("photoCaptionID0").innerHTML = x[1];
break;
}
}

</script>

He thought he saw a Bufffalo
Upon the chimney-piece:
He looked again, and found it was
His Sister's Husband's Niece.
"Unless you leave this house," he said,
"I'll send for the Police!"
- Lewis Carroll

thesprucegoose
03-16-2009, 06:33 PM
Thanks for the reply.

I was moving towards something like that. Unfortunately, it's not working.

http://www.wildearthride.com/hexCode/javascript_sort.html

This is the source code. See anything funky?

thesprucegoose
03-16-2009, 06:38 PM
Just realized that the variable photoCaptionID0 doesn't match captionID[0]

I changed all the variables to 1, which does match, and got the same results.

Philip M
03-16-2009, 06:59 PM
I am afraid that I don't really understand what you are trying to do. It works very well for me. I did change the values around

captionID[2] = "13006, Item 3"; //match

for demonstration purposes. You should get "Item 3" written to the <div>.

thesprucegoose
03-16-2009, 07:11 PM
That's strange. It's not working for me at all. But .innerHTML hasn't been working all day for some strange reason...

I'm assuming it doesn't matter if you stick the JS in the head or the body. Am I correct in that assumption?

I'm gonna try a system reboot and hope that solves the issue.

--thesprucegoose

freedom_razor
03-16-2009, 07:15 PM
It does matter. Move the script below those DIVs in the markup and it is going to work [well, after you change those 0 to 1, there are still 0s there on the page you linked to]

thesprucegoose
03-16-2009, 07:19 PM
I got an error in fire bug that says "document.getElementById("photoCaptionID0") is null"

freedom_razor
03-16-2009, 07:21 PM
That's because you still didn't move your script out of the HEAD into the BODY below the DIV with that id you see in the error message.

thesprucegoose
03-16-2009, 07:22 PM
Ok cool got it.

Thanks are in order!

--thesprucegoose

thesprucegoose
03-16-2009, 07:23 PM
Sorry freedom_razor, I posted the error message before I saw your post.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum