Link to home
Create AccountLog in
Avatar of Waldir-PRG
Waldir-PRGFlag for Brazil

asked on

How to dynamically create a function receiving 'window.event'?

I have the following code:

      var oTab = document.getElementById("tbGrdCob");
      var oRow = oTab.insertRow(0);
      oRow.style.color='#333333';
      oRow.align="center";
      oRow.className="grid_RowStyle";
      oRow.onmouseover=function(){grdLinha(this,0);};
      oRow.onmouseout=function(){grdLinha(this,1);};
      var oCol0 = oRow.insertCell(0);
      oCol0.setAttribute("align", "center");
      oCol0.setAttribute("style", "width:90px;");
         
      var oTextD = document.createElement("input");
      oTextD.className="edicao_centralizado";
      oTextD.style.width='80px';
      oTextD.maxLength='10';
      oTextD.onkeypress = function() { jslTransformData(window.event, this) };
      oCol0.appendChild(oTextD);

      var oCol1 = oRow.insertCell(1);
      oCol1.setAttribute("align", "center");
      oCol1.setAttribute("style", "width:92px;");
      ...


   function jslTransformData(e, o) {
      var keyCode = (e.keyCode ? e.keyCode : (e.which ? e.which : e.charCode));
      alert(keyCode);
   }


In IE it's ok! But not working in Firefox.
e is undefined on function!

What am I doing wrong?

Avatar of Hagay Mandel
Hagay Mandel
Flag of Israel image

The event.which is for  jQuery scripts, and is for avoiding browser differences.
Since you are not using JQuery, the 'which' property is undefined!
Avatar of Waldir-PRG

ASKER

Thanks by your answer!

I understand, but the error 'undefined' happens in the passage of the parameter 'window.event' before the verification of 'e.which'.
Apparently not!
You say yourself: "In IE it's ok! But not working in Firefox."
More:
window.event is not supported by FF, you need a workaround:
Declare a variable:
var eventVar=window.event? event : e
And then use 'eventVar' hereafter.
Thanks again!

I'll explain: This function 'TransformData (event, this)' I use throughout the event 'onkeypress' but directly on the element 'input'. Example:

<input name="txtData" type="text" maxlength="10" id="txtData" class="edicao_centralizado" onkeypress="return TransformData(event, this);" onBlur="jsValidarData2(this);" style="width:90px;" />

When used in this way, it works fine in IE and Firefox also. However, the element must be created dynamically, as follows:

      var oTextD = document.createElement("input");
      oTextD.className="edicao_centralizado";
      oTextD.style.width='80px';
      oTextD.maxLength='10';
      oTextD.onkeypress = function() {TransformData(event, this) };
      oCol0.appendChild(oTextD);

This way does not work in firefox, only IE.
Continuing ...

In Firefox is not transmitted the 'event', gets'undefined'. But only when I create dynamically.

Thanks,
Seems like your script creates a field that has no name attribute, and therefore the event.this is undefined.
In addition, I think DOM requires a different syntax:
try { // For IE method
  input = document.createElement('<input type="text" name="myInput">');
}
catch (error) { // DOM method

var oTextD = document.createElement("input");
      oTextD.setAttribute('Name','myInput');
      oTextD.setAttribute('className','edicao_centralizado');
      oTextD.setAttribute('style.width', ''80px');
      oTextD.setAttribute('maxLength','10');
      oTextD.onkeypress = function() {TransformData(event, this) };
      oCol0.appendChild(oTextD);
}
Correction:

try { // For IE method
  input = document.createElement('<input type="text" name="myInput">');
}
catch (error) { // DOM method

var oTextD = document.createElement("input");
      oTextD.setAttribute('Name','myInput');
      oTextD.setAttribute('class','edicao_centralizado');
      oTextD.setAttribute('width', ''80px');
      oTextD.setAttribute('maxLength','10');
      oTextD.onkeypress = function() {TransformData(event, this) };
      oCol0.appendChild(oTextD);
}
SOLUTION
Avatar of Badotz
Badotz
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
You will still have to determine the key code:


function jslTransformData(e, o) {
    var ev = resolve(e);
    var keyCode = (ev.source.keyCode ? ev.source.keyCode : (ev.source.which ? ev.source.which : ev.source.charCode));
    alert(keyCode);
}

Open in new window

Sorry:


function jslTransformData(e) {
    // etc.
}

Open in new window

Thanks everyone,

Now it's working right.
I used solution mentioned by Badotz.


//oTextD.onkeypress = function() { jslTransformData(window.event, this) };
oTextD.onkeypress = jslTransformData;
oCol0.appendChild(oTextD);

...

function jslTransformData(e) {
   var ev = resolve(e);
   if (ev.source) {
      return TransformData(ev.source, ev.target);
   }
}

Open in new window

No worries - glad to help.