I'm trying to set the "onclick" attribute of a link generated with jQuery.
While other attributes work without problem (eg. href, but also other custom attrs), setting "onclick" seems to only work in FireFox. On other browsers the attribute just gets ignored.
Any hint? Is this behaviour correct? And why?
<html><head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
jQuery(function(){
var obj = $('<a/>');
obj.html('click me');
obj.attr({'href':'#test', 'onclick':'alert("o hai"); return false;'});
$('body').append(obj);
});
</script>
</head><body /></html>
EDIT: I know about the .click() jQuery method; but that's not the point, I just want to set the attribute and can't figure out why it doesn't work like any other attr does.