SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member Alterscape's Avatar
    Join Date
    Nov 2003
    Location
    NY, USA
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    xAddEventListener and fxns w/Arguments

    Okay, let's say I want to use xAddEventListener to trigger onButtonMouseOver(whichButton), a function which accepts an integer to tell it which button has been mouseovered (so that the proper info can be retrieved from an array). If I use the line

    HTML Code:
    xAddEventListener('leftmenu00_sensor', 'mouseover',buttonMouseOver(0),false);
    it won't work at all. However

    HTML Code:
    xAddEventListener('leftmenu00_sensor', 'mouseover',buttonMouseOver,false);
    works fine, so long as I hardcode that leftmenu_00 is being moused over in the buttonMouseOver function.

    Since I really don't want to write a different function for every button on my page, I'd really like to know how to pull this off.

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Alterscape,

    This might not be the best solution for you since I'm not sure what else you're doing. But it should give you some ideas:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title>xAddEventListener Demo</title>
    <script type='text/javascript' src='x.js'></script>
    <script type='text/javascript'>
    var veggies = new Array('Corn', 'Peas', 'Beans', 'Squash', 'Potatoes', 'Tomatoes');
    window.onload = function() {
      var b;
      for (var i = 0; i < veggies.length; ++i) {
        b = xGetElementById('btn' + i);
        b.veggieIndex = i;
        xAddEventListener('btn' + i, 'mouseover', btnOnMouseover, false);
      }
    }
    function btnOnMouseover(evnt)
    {
      var e = new xEvent(evnt);
      var b = e.target;
      if (typeof(b.veggieIndex) != 'undefined') {
        var t = xGetElementById('txt0');
        t.value = veggies[b.veggieIndex];
      }
    }
    </script>
    </head>
    <body>
    
    <form>
    <p><input id='btn0' type='button' value='Corn' /></p>
    <p><input id='btn1' type='button' value='Peas' /></p>
    <p><input id='btn2' type='button' value='Beans' /></p>
    <p><input id='btn3' type='button' value='Squash' /></p>
    <p><input id='btn4' type='button' value='Potatoes' /></p>
    <p><input id='btn5' type='button' value='Tomatoes' /></p>
    <p><input id='txt0' type='text' value='' /></p>
    </form>
    
    </body>
    </html>


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
  •