views:

299

answers:

2

hi,

i'm trying to capture keystrokes inside my prototype function, here's my code:

function txtBox(input) // pass textbox
{
     this.id = "myTextbox";
     this.txt = input
}
txtBox.prototype.init = function()
{
     this.txt.bind("keyup",this.keyup);
}
txtBox.prototype.keyup= function(event)
{
     alert("keycode: event.keyCode);
     alert(this.id);
}
var myTxt = new txtBox($(#txt)); // create object
myTxt.init();

capturing works but the problem is that the keyup triggers "outside" my object, which means this.id returns "undefined" although it was defined.

anyone knows how to keep consistency with this?

thx, fuxi

A: 

I tried copy pasting your code into my firebug and running it on this page (changed the alerts to console.log though). And it seems to work just fine. It captures every keydown in the textfield I selected, but not anywhere else.

googletorp
A: 

Two problems I see, assuming you have an actual tag named "txt" in your script as so:

<input type="text" id="txt"/>

this line needs to be change from

alert("keycode: event.keyCode);

to

alert("keycode": event.keyCode);

also this needs to be changed from

new txtBox($(#txt));

to

new txtBox($('#txt'));

and it will alert your textbox name, I dont believe in IE you will be able to change the id the way you're trying to change it doing this.id="textbox"

TStamper