views:

253

answers:

2

Hey Everyone,

I just came across this weird problem that is happening in IE. In the html I am adding an onclick event to an element that calls a function to change a page like this,

onclick="changePage(1, 2);"

Inside the changePage function I am changing the currentPageNum, and nextPageNum of the forward and backwards buttons when the function is called like this,

function changePage(currentPageNum, nextPageNum) {

  var pageValidated = true;

  //If the current page content validates, then we can change the page
  if(pageValidated) {
    //Add new events to both arrow buttons
    $('#goBack').attr('onclick', '').unbind().click(function() {
        changePage(nextPageNum, parseInt(nextPageNum) - 1);
    });

    $('#goForward').attr('onclick', '').unbind().click(function() {
        changePage(nextPageNum, parseInt(nextPageNum) + 1);
    });
  }
}

I added the attr('onclick', '').unbind() in order to remove the old onclick event and then add the new one. This is working fine in FF, but in IE it is calling both the new click event and the old at the same time for some reason. I thought that when you attach a new click event that the old one went away, but that does not seem to be the case now. How can I get this working in IE?

I think I know whats happening now. When the changePage function is called, it is removing the old onclick event, adding the new one after the first onclick event and then calling the onclick event that was just added directly after the first one was called. It is acting as if I have 2 function calls inside the same onclick event.

I am using jQuery version 1.4.2. I cut out extra content from this function to simplify it, but this is the part that is causing the problem.

Thanks for any help, Metropolis

A: 

The correct way to unbind an event using jQuery is:

$('#goBack').unbind("click").click(function() {
    changePage(nextPageNum, parseInt(nextPageNum) - 1);
});

if for some reason that still fails you can try:

$("#goBack").click(undefined);

or:

document.getElementById("goBack").click = undefined;
karim79
But doesnt unbind() unbind all events?
Metropolis
and dont you have to add the attr('onclick', '') to fix the problem with some browsers not removing the events?
Metropolis
I tried your way and I am still having the same problem.....
Metropolis
@Metropolis - I'm not sure, that's the first I've heard of that. I use `.unbind("someEvent")` frequently without ever facing problems.
karim79
could jQuery 1.4.2 have anything to do with it?
Metropolis
The 2nd suggestion is causing an error "stack overflow at line: 0"
Metropolis
Did you try the third? If it's not that, maybe it is something to do with an inline onclick handler. I would try binding to your elements using `$(".something").click(function() {`. And lastly, is your code within a `$(document).ready(function() {...`
karim79
The third one is doing the same thing....Very strange stuff. It is not inside a ready event.
Metropolis
A: 

I believe that the best way to fix this problem is to not create anymore inline javascript events along with jQuery events. My guess is that this causes jQuery to not know what events are taking place elsewhere, so both events get run. What I should do here instead is put the following code into an external javascript file

$(function() {
    $('#ele').click(function() {
        changePage(1, 2);
    });
});

This will allow the event to be changed by jQuery whenever it gets inside the changePage function. Please correct me if this is wrong logic.

Metropolis

Metropolis