Would it be possible for me to dynamically add an event handler to a form field. I need to do this, because in the scope of the work I am doing – the form field to which this is added is chosen dynamically.
So, I want to know if something like this would work:
document.forms['Form1'].elements['chosen_field'].onkeydown = some_function();
EDIT:
Another sub-question here. I want to pass on the event to some_function(), but do not want to return anything back. Is there a way I can do that? Essentially I am using some_function() to update some JS variable – and for that I need to find out what key was pressed.
When you assign event handlers like this
You are saying assign whatever
some_functionreturns to this keydown event.You need to drop the
()from it so you assign a reference to the function.