While reading the book jQuery in Action and playing with the first events handling example, I discovered that jQuery doesn't consistently clone DOM Level 0 events. It clones the inline events, but not those defined as a property. It could be a good thing to discourage the use of inline event handlers, but is is by design? What is one wants to clone something from a legacy page ?
Here is an alteration of the example given in the book, to demonstrate the behavior.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>DOM Level 0 Events Example</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js">
</script>
<script type="text/javascript">
$(function(){
$('#testElement')[0].onclick = function(event) {
say(this.id + ' Click ASSIGN [DOM 0]');
}
$('#testElement').click(function() { say(this.id + ' Click JQUERY [DOM 2]');});
$('#testElement').clone(true).attr('id',"clonedElement").insertAfter($('#testElement'));
});
function say(text) {
$('#console').append('<div>'+text+'</div>');
}
</script>
</head>
<body>
<div id="testElement" style="border: solid brown 1px; margin: 10px; width : 100px; height: 100px;" onclick="say(this.id + ' Click INLINE [DOM 0]')"> </div>
<div id="console"></div>
</body>
</html>