I’m new to Javascript and I’m trying to make the following code scan to see if a textbox with an id=”lessonNum” is active, if it is not i would like to send a .click to a submit button with an id=”A” when I press ‘a’ on the keyboard. Right now when I select the textbox I get an alert, but when I don’t have it selected it doesn’t pick up my keydown. Please Help!
function GetActive () {
if (document.activeElement.id == 'lessonNum') {
alert('lessonNum is active');
var b1=new Boolean(1);
} else {
var b1=new Boolean(0);
}
}
document.addEventListener("keydown", keyDownTextField, false);
function keyDownTextField(e) {
var keyCode = e.keyCode;
if(keyCode==65) {
if(b1==0) {
alert('a has been pressed');
document.getElementById('A').click();
}
}
}
In your code:
the above line creates a local variable called b1 and assigns a new boolean object. I think you just want a primitive, so:
or the whole if..else statement can be replaced with:
Note that
getActiveis never called sob1is never set anyway.In keyDownTextField you have:
however
bis local toGetActiveso a reference error will be thrown. The simple solution is to makebglobal, a little more work but better though to hold it in a closure.e.g.