I am trying to set the onclick attribute for a link generated using jQuery.
While other attributes work without problems (for example, href, but also other user attributes), setting " onclick " only works in FireFox . In other browsers, the attribute is simply ignored.
Any hint? Is this behavior right? 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 jQuery.click () method; but it’s not, I just want to set the attribute and cannot understand why it doesn’t work, like any other attr.
source
share