SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jun 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question How to pass particular array index as parameter in a function??

    I don't know if it is possible to pass a particular array index rather than passing the entire array as parameter in a function? If yes then how is it possible? And if not what could be the alternative??

    The function called here is executed by some other function.
    Code:
    var catElementArr = new Array();
    var lensDiv = new Array();
    var lensDiv = false;
    var catLens = new Array();
    var catLens = false;
    var j = false;
    var p = 0;
    
    function setShelf(theCatDiv) {
    var catDivId = theCatDiv.getAttribute("id");
    var divParent = theCatDiv.parentNode;
    var centerDiv = divParent.getElementsByTagName("div")[1];
    var centerDivId = centerDiv.getAttribute("id");
    if(catLens) {
    p++;
    catLens[p] = divParent.getElementsByTagName("div")[3];
    }
    else {
    catLens[p] = divParent.getElementsByTagName("div")[3];
    }
    for (j=0; j<catElementArr.length; j++) {		
    if (catDivId == catElementArr[j].nodeName) {
    var catCompList = catElementArr[j].getElementsByTagName("company");
    for (var k=0; k<catCompList.length; k++) {
    var currentComp = catCompList[k];
    var createCompDiv = document.createElement("div");
    createCompDiv.setAttribute("class", "compDiv");  
    createCompDiv.setAttribute("className", "compDiv");  
    var lensDivImg = currentComp.getElementsByTagName("lensImage")[0].firstChild.data;  
    createCompDiv.setAttribute("lensImage", lensDivImg);  
    createCompDiv.setAttribute("onmouseover", "loadLens(this, catLens[p])");  //catLens[index] is not getting passed
    createCompDiv.setAttribute("onmouseout", "emptyLens()");     
    var createAnchor = document.createElement("a");
    var linkInfo = currentComp.getElementsByTagName("hLink")[0].firstChild.data;  
    createAnchor.setAttribute("href", linkInfo);  
    createAnchor.setAttribute("class", "a");  
    createAnchor.setAttribute("target", "_blank");  
    createCompDiv.appendChild(createAnchor);    
    var createImage = document.createElement("img");
    var imgSource = lensDivImg; 
    createImage.setAttribute("src", imgSource);  
    createImage.setAttribute("class", "compImg");  
    createImage.setAttribute("className", "compImg");
    createImage.setAttribute("width", 13");
    createImage.setAttribute("height", "13");
    createImage.setAttribute("alt", "AD");  
    createAnchor.appendChild(createImage);			centerDiv.appendChild(createCompDiv);	
    }	
    }
    }
    }
    
    function loadLens(whichDiv, catLens[p]) //catLens[index] becomes undefined parameter for this function 
    {
    var lensDivImgSrc = whichDiv.getAttribute("lensImage");
    lensDiv[j] = catLens[p];
    var createLensImg = document.createElement("img");
    createLensImg.setAttribute("src", lensDivImgSrc);
    lensDiv[j].appendChild(createLensImg); 
    }
    
    function emptyLens() 
    {
    lensDiv[j].innerHTML = "";
    }

  2. #2
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you wanting to pass the index (i.e., the ordinal "address" of an array element) or the value of the array element at that index?

  3. #3
    SitePoint Member
    Join Date
    Jun 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I want to pass the value stored at that array index but i don't know if this is the right way to do it???

  4. #4
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here are a couple of problems I see right off.

    First, getElementsByTagName returns a NodeList object, not an array. You select an individual node from the list using the NodeList's item method. So, instead of element.getElementsByTagName(tagname)[index], it should be element.getElementsByTagName(tagname).item(index).

    Second, setting an event handler with setAttribute does not usually work in all browsers. It is better to set the handler by passing a reference to the handler function like so:

    Code:
    createCompDiv.onmouseover = loadLens;
    Unfortunately, you cannot pass arguments to the handler in this way. Your best bet would be to set a global variable, then write your handler function so that it queries the global variable. Another problem related to this one is, the keyword this only exists within object scope and refers to the object in whose scope it is referenced. In other words, you cannot pass this to a function called from the global scope.

    I also see several tag names and attributes in your code that are not standard (X)HTML elements or attributes, e.g., attribute "className", element "lensImage", attribute "lensImage", and element "hLink". The HTML parser will not know what these mean and will probably issue an error message.

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    The typical way of doing this is to pass it like this:

    Code javascript:
    createCompDiv.onmouseover = function () {
        loadLens(this, catLens[p]);
    }
    ...
    function loadLens(whichDiv, catLensValue) {
        ...
    }

    There may be trouble though when the onmouseover event it fired, because only at that time does it get and use the catlens[p] variable.

    If that doesn't become a problem, you can pass the value to an anonymous function, which is then assigned to the onmouseover variable.

    Code javascript:
    createCompDiv.onmouseover = function (catLensValue) {
        return function () {
            loadLens(this, catLensValue);
        }
    }(catLens[p]);
    ...
    function loadLens(whichDiv, catLensValue) {
        ...
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •