Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Mar 2009
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Comparing Arrays

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

    Thanks much!

    --thesprucegoose

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Try this:-

    Code:
    <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
    Last edited by Philip M; 03-16-2009 at 06:28 PM.

  • #3
    New Coder
    Join Date
    Mar 2009
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks for the reply.

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

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

    This is the source code. See anything funky?

  • #4
    New Coder
    Join Date
    Mar 2009
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    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>.

  • Users who have thanked Philip M for this post:

    thesprucegoose (03-16-2009)

  • #6
    New Coder
    Join Date
    Mar 2009
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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

  • #7
    Regular Coder
    Join Date
    Dec 2008
    Location
    Tannhäuser Gate
    Posts
    286
    Thanks
    7
    Thanked 58 Times in 57 Posts
    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]
    Last edited by freedom_razor; 03-16-2009 at 07:18 PM.

  • Users who have thanked freedom_razor for this post:

    thesprucegoose (03-16-2009)

  • #8
    New Coder
    Join Date
    Mar 2009
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I got an error in fire bug that says "document.getElementById("photoCaptionID0") is null"

  • #9
    Regular Coder
    Join Date
    Dec 2008
    Location
    Tannhäuser Gate
    Posts
    286
    Thanks
    7
    Thanked 58 Times in 57 Posts
    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.

  • Users who have thanked freedom_razor for this post:

    thesprucegoose (03-16-2009)

  • #10
    New Coder
    Join Date
    Mar 2009
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Ok cool got it.

    Thanks are in order!

    --thesprucegoose

  • #11
    New Coder
    Join Date
    Mar 2009
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Sorry freedom_razor, I posted the error message before I saw your post.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •