And also, how to remove it then?
views:
30answers:
2
+1
A:
// we're in some internal scope here
var x = 10;
var fn = function( e ) {
wrappedFunction( e, x );
}
//add
o.addEventListener( 'click', fn, false );
// create remover
var remover = function() {
o.removeEventListener( 'click', fn, false );
}
//save the remover for later or return it - when it's called from whatever scope the event is removed
remover();
meouw
2010-02-05 15:40:27
Doesn't work cross-browser. You'd have to branch with attachEvent for IE.
Marco
2010-02-05 17:28:51
A:
When you say "attributes", do you mean arguments/parameters?
If so, you can dynamically assign an event handler that does accept arguments. In the example below, the argument testValue is passed to the dynamically assigned event handler:
<html>
<head>
<title>Test</title>
</head>
<body>
<input id="testInput" type="text"/>
<script type="text/javascript">
var testValue = "Success.";
document.getElementById("testInput").onkeydown = function() {
test(testValue); }
function test(testValue) {
alert(testValue);
}
</script>
</body>
</html>
To remove the event handler, you can assign it to null:
document.getElementById("testInput").onkeydown = null;
Abboq
2010-02-05 15:45:15