SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: array help

  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    array help

    Let's say I predefine some arrays.

    Code:
    var orange = new Array('orange','fruit','round','peel');
    var apple = new Array('apple','fruit','odd','skin');
    
    function whicharray(arrayiwant){
    ?
    }
    Just making the above up.

    But let's say I want to access one of these with a link.

    HTML Code:
    <a href="#" onclick="javascript:whicharray('orange');">something</a>
    How can I carry over which array I'm looking for and access that array? When I call it from that function, it just returns orange as a string, I'm not sure how to tell it I want that array.

  2. #2
    $books++ == true matsko's Avatar
    Join Date
    Sep 2004
    Location
    Toronto
    Posts
    795
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are trying to make a function which takes the array name as a parameter and then returns it, then with normal coding it might not be possible.

    But...

    using eval() it might just work.

    Code:
    var orange = new Array('orange','fruit','round','peel');
    var apple = new Array('apple','fruit','odd','skin');
    
    function whicharray(arrayiwant)
    {
    var ar;
    eval("ar ="+arrayiwant);
    return ar;
    }
    
    although, I don't suggest using the eval() function due to it's lack of efficiency.
    I can't believe I ate the whole thing

  3. #3
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, you do not need eval(). I explain the solution in this thread.

    jjjamiem, the "javascript:" in the onclick handler is useless. You should remove it. Also, it's best to avoid using inline event handlers. I suggest you read up on Unobtrusive JavaScript and other JavaScript Best Practices.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  4. #4
    SitePoint Wizard Mike Borozdin's Avatar
    Join Date
    Oct 2002
    Location
    Edinburgh, UK
    Posts
    1,743
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What exactly do you want the function to return the name of array? Any reason for that?

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kravvitz View Post
    No, you do not need eval(). I explain the solution in this thread.

    jjjamiem, the "javascript:" in the onclick handler is useless. You should remove it. Also, it's best to avoid using inline event handlers. I suggest you read up on Unobtrusive JavaScript and other JavaScript Best Practices.
    The code was just copy & paste from another example, I'm not using javascript and have a real href, the onclick event is just the preference for anyone who has js on; and thanks for the answer, appreciated.
    Quote Originally Posted by Mike Borozdin View Post
    What exactly do you want the function to return the name of array? Any reason for that?
    Accessing certain information without using ajax.

  6. #6
    SitePoint Wizard Mike Borozdin's Avatar
    Join Date
    Oct 2002
    Location
    Edinburgh, UK
    Posts
    1,743
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Accessing certain information without using ajax.
    It's still not clear.

    The basic code that determines the array is:
    Code:
    var orange = new Array('orange','fruit','round','peel');
    var apple = new Array('apple','fruit','odd','skin');
    
    function whicharray(element){
        var arr;
        for(var i = 0; i < 4; i++) {
            if(orange[i] == element) {
                arr = "orange";
                window.alert(arr);
                return arr;
            } else if(apple[i] == element) {
                arr = "apple";
                window.alert(arr);
                return arr;
            }
        }
    }


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
  •