Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Having trouble passing array into an event handler

  • This topic is empty.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • #32519
    msevrens
    Member

    I’m having trouble passing an array into an event handler.

    This does not work:

    Code:
    var lightbulb = [1,1,1,0,1,0]
    var banana = [0,0,1,1,0,1]

    function changetextimage(arr){
    for(var i=0;i<5;i++){
    var pixel = document.getElementById(“text”+i);
    if (arr) {
    pixel.style.color=”#000000″;
    }
    else {
    pixel.style.color=”#FFFFFF”;
    }
    }
    }

    $(‘#lightbulbpicture’).mouseover(changetextimage(lightbulb));
    $(‘#bananapicture’).mouseover(changetextimage(banana));

    This does work:
    Code:
    var lightbulb = [1,1,1,0,1,0]
    var banana = [0,0,1,1,0,1]

    function changetextimage1(){
    for(var i=0;i<5;i++){
    var pixel = document.getElementById(“text”+i);
    if (lightbulb
    ) {
    pixel.style.color=”#000000″;
    }
    else {
    pixel.style.color=”#FFFFFF”;
    }
    }
    }

    function changetextimage2(){
    for(var i=0;i<5;i++){
    var pixel = document.getElementById(“text”+i);
    if (banana
    ) {
    pixel.style.color=”#000000″;
    }
    else {
    pixel.style.color=”#FFFFFF”;
    }
    }
    }

    $(‘#lightbulbpicture’).mouseover(changetextimage1);
    $(‘#bananapicture’).mouseover(changetextimage2);

    I have a whole bunch of these arrays (which are about 4000 characters longer) that I want to pass into this function so I just want to pass each as an argument rather than write each function out.

Viewing 1 post (of 1 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.