@Markus
Yes, except that the syntax would be something like:
- var anevent = 'onclick';
-
document.getElementById('id')[anevent] = function() {alert("it works");}
-
Changes:
1. changed event to anevent. IE seems to have problems with setting variables with the name 'event' (messes with the global window.event).
2. Use bracket notation for strings.
3. Use a function object, otherwise it would alert "it works" as soon as this event is attached.
4. You forgot to close the string ;)
Note that you could alternatively use addEventListener/attachEvent (look them up).