<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" />
<div id="div1">Click here</div>
</div>
</form>
<script type="text/javascript">
var $ = function(e) { return document.getElementById(e); };
var pageDefault = {
btn1: $('Button1'),
testfx: function() {
alert('test');
},
init: function() {
this.btn1.onclick = function() {
this.testfx();
return false;
}
}
}
pageDefault.init();
</script>
</body>
Why won’t button1 click event fire?
It may happen that you need to use
$('<%= Button1.ClientID %>')instead of$('Button1').See this article for more details.
Another issue, mentioned in other answers, is that event handler refers to
thiswhich iswindow. To fix that you could rewrite your handler in the following manner: