antonioh antonioh - 9 months ago 58
Javascript Question

jquery unbind click

I'm going nuts just trying to unbind an onclick handler from the event in jQuery so I can bind it later on to another function.

I have isolated the code in a test page so there's nothing but the meat, just a button that calls a function and a script that tries to unbind it:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html xmlns="">
<head id="Head1" runat="server">
<script src="../../Scripts/jquery-1.3.2.min.js" type="text/javascript">

<script src="../../Scripts/jquery-1.3.2.js" type="text/javascript">

<script type="text/javascript" language="javascript">
$(document).ready(function() { $("#btnNext").unbind('click'); });
function hi() {window.alert("hi");}


<input id="btnNext" type="button" value="Next" onclick="hi();" />

Anybody knows why the click event keeps calling the hi() function no matter what I said in the document.ready?



Because you put it in the html attribute, it stays there. It was not bound with jQuery so jQuery is not tracking it's usage.