1

I'm having problem trying to remove the default click event handler from a tag. I want in the end to add another handler. I tried the following but didn't work :

My HTML head

<script src="/Scripts/jquery-2.0.3.js"></script>

The html body

<aside id="content">
    <a id="goNext" href="/Home/NextPage?pageIndex=1">Next page</a> 

    <div>Page number : 0</div>
</aside>

The script

<script type="text/javascript">
    $("#goNext").unbind("click");
    $(window).off("click", "#goNext", false);
    $("#goNext").off("click");
</script>

NB : I'm using jQuery 2.0.3

SidAhmed
  • 2,192
  • 2
  • 21
  • 42
  • 3
    I think you'll have to post more code. How are you binding the event handler in the first place? – Pointy Aug 14 '13 at 14:47
  • 2
    possible duplicate of [Best way to remove an event handler in jQuery?](http://stackoverflow.com/questions/209029/best-way-to-remove-an-event-handler-in-jquery) – Sergio Aug 14 '13 at 14:48
  • @j08691 a typing error – SidAhmed Aug 14 '13 at 14:50
  • If that's the whole script, *there's no click event bound to the link in the first place*. Seriously, you need to show the event you're trying to remove. If you're trying to prevent the link from navigating to the page in its href, go with Zac's answer. – JJJ Aug 14 '13 at 14:54
  • @Juhana : I'm trying to remove the default handler – SidAhmed Aug 14 '13 at 15:00
  • `$('#goNext').on('click', false);` – Ja͢ck Aug 14 '13 at 15:02
  • Yeah, ok, that's not a handler at all. It's just the default behavior. The first part of Zac's answer is correct then. – JJJ Aug 14 '13 at 15:02
  • Since it handle a event (a click event), shouldn't we call it a handler ? – SidAhmed Aug 14 '13 at 15:03
  • No. An event handler has a [specific meaning](https://developer.mozilla.org/en-US/docs/Web/API/Event) which doesn't include browser default behavior. – JJJ Aug 14 '13 at 15:06

1 Answers1

3

It seems that you want to stop the default click action from taking place:

$("#goNext").click(function (e) {
    e.preventDefault();
});

Also put your jQuery in a ready statement: http://api.jquery.com/ready/

<script type="text/javascript">
    $(document).ready(function() {
        $("#goNext").click(function (e) {
            e.preventDefault();
        }); 
    });
</script>
Zac
  • 1,663
  • 1
  • 18
  • 22
  • 2
    I initially downvoted this, but I took it back. This might actually be what OP is asking; the question is totally unclear. – Mathletics Aug 14 '13 at 14:48
  • 1
    If the first part of your answer is correct. The second part obviously makes no sense at all. – Ja͢ck Aug 14 '13 at 14:58