Re: assigning a function with variables to an element, how to do it?

Giganews Newsgroups
Subject: Re: assigning a function with variables to an element, how to do it?
Posted by:  windandwaves (nfranck…@gmail.com)
Date: Thu, 12 Jul 2007

On Jul 13, 10:42 am, windandwaves <nfranc.…@gmail.com> wrote:
> Hi Folk
>
> How can I rewrite the function below so that it works.  Problem is I
> can't get the functions assigned properly to the al element.
>
> What am I doing wrong?
>
> var buttonNames = new Array("", "plus", "minus","left", "up",
> "down","right", "reset" );
>
> var buttonFunction = new Array("", "zoom", "zoom", "scroll",
> "scroll","scroll", "scroll", "");
>
> var buttonLowSpeed = new Array(99, 3, 3, -3, -3, 3, 3, 99);
>
> var buttonHighSpeed = new Array(99, 9, 9, -9, -9, 9, 9, 99);
>
> var buttonDirection = new Array(99, 1, -1, 0, 1, 1, 0, 99 );
>
> function init() {
>
>  for(var i = 1; i < 8; i++ ) {
>
>  var al = document.getElementById("a" + buttonNames[i]);
>
>  al.onclick = function () {return false;}
>
>  al.onmousedown = function(){buttonFunction[i] + "(" +
> buttonHighSpeed[i] + ", " + buttonDirection[i] + ",0," + i + ");"}
>
>  al.onmouseover = function() {buttonFunction[i]
> ( buttonLowSpeed[i],buttonDirection[i],0,0);}
>
>  al.onmouseup = function() {"stop(" + i + ");";}
>
>  al.onmouseout = function () {"stop(" + i + ");";}
>
>  }
>
> }
>
> function zoom(A,B, C, D){
> ...}
>
> function scroll (A, B, C, D) {
> ...
>
> }

I fixed it!

  var al = document.getElementById("a" + buttonNames[i]);
  var fO = new Function(buttonFunction[i] + "(" + buttonLowSpeed[i] +
"," + buttonDirection[i] + ",0,0);");
  var fD = new Function(buttonFunction[i] + "(" + buttonHighSpeed[i]
+ ", " + buttonDirection[i] + ",0," + i + ");");
  var fC = new Function("return false;");
  var fU =  new Function("stop(" + i + ");" + buttonFunction[i] + "("
+ buttonLowSpeed[i] + "," + buttonDirection[i] + ",0,0);");
  var fX = new Function("stop(" + i + ");");
  al.onmouseover = fO;
  al.onmousedown = fD;
  al.onclick = fC;
  al.onmouseup = fU;
  al.onmouseout = fX;

Replies

In response to

assigning a function with variables to an element, how to do it? posted by windandwaves on Thu, 12 Jul 2007