SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2002
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    get array position?

    hi, i have an array like this in Javascript

    var allLayersArray = new Array()
    PHP Code:
    allLayersArray[0] = 'teams';
    allLayersArray[1] = 'news';
    allLayersArray[2] = 'coaches';
    allLayersArray[3] = 'aboutus';
    allLayersArray[4] = 'rinks';
    allLayersArray[5] = 'hockeyinfo';
    allLayersArray[6] = 'supporters';
    allLayersArray[7] = 'credits'
    and i want to pull out the right number if say send in the variable into my function of 'teams' and i want to get out the '0'.

    how could i do that? i know this has to be really simple, i just can't remember how to do it.

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here ya go
    Code:
    function getLayerID(label)
    	{
    	for (var i=0; i<allLayersArray.length; i++)
    		{
    		if (allLayersArray[i] == label)
    			return i;
    		}
    	}
    
    var layerID = getLayerID('teams');
    Last edited by beetle; Jul 23, 2002 at 12:07.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    SitePoint Zealot
    Join Date
    Feb 2002
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah, thank you.

    actually, i just remembered what to do seconds after posting, but thank you for helping though!

  4. #4
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should declare the array in one line though - to save
    bytes.

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  5. #5
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Flawless_koder
    You should declare the array in one line though - to save
    bytes.
    i.e.
    Code:
    var allLayersArray = new Array('teams','news','coaches','aboutus','rinks','hockeyinfo','supporters','credits');
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  6. #6
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes.

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---


Bookmarks

Posting Permissions

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