jQuery click handlers don't always work in IE 7/8, using v1.5

930 views Asked by At

I have defined some click event handlers as follows:

$(document).ready(function () {
  /*
  * Since the "Start Upload" button is available, we don't need to worry about starting the 
  * upload when the user clicks "Update". But we need to make sure to display an error message
  * if there are files in the uploader that have not been uploaded yet.
  */
  $('#myAccount p.buttons input[alt="Update"]').click(function (event) {

    // check if there are files in the uploader
    if (uploader.files.length > 0) {

        // if there are files that have not been uploaded yet, we need to show an error message
        if (uploader.total.uploaded != uploader.files.length) {

            // if the error message hasn't been created yet, create it
            // else, it'll already be visible so we don't need to do anything
            if ($('#upload_error').length == 0) {
                $('<p id="upload_error">Error!</p>').insertAfter('#myAccount p.buttons input[alt="Cancel"]');
            }
            event.preventDefault(); // stop the click event
        }
    }
    // continue click event as normal
  });
});

... and ...

// if the cancel button is clicked, then remove the files from the uploader
$('#myAccount p.buttons input[alt="Cancel"]').click(function (event) {
    uploader.splice(0, uploader.files.length);
    // continue click event as normal
});

Both work fine in Firefox, but in IE8 and IE7 (compatibility mode), these do not work all of the time.

To be a bit more specific, this "uploader" stuff is related to the Plupload file uploader. Essentially, I have this uploader within a form. The form submits fine and the above click handlers work fine if I don't touch the uploader at all.

However, in the following case, the above click handlers don't work: I queue files and let the uploader do its thing, so all files have been uploaded; now I click submit on the form and nothing happens, but I expect the form to submit. Anytime I interact with the uploader, the click event handlers don't work.

I hoping my comments clarify my intentions. Any ideas as to why this isn't working in IE 7/8? What am I doing wrong? Is the event.preventDefault() handled differently in IE?

Thanks.

2

There are 2 answers

0
Hristo On BEST ANSWER

I just switched to the Flash runtime. It seems to be working okay for now.

6
Andrew Jackman On

Try this:

if (event.preventDefault) { event.preventDefault(); } else { event.returnValue = false; }

test for preventDefault before using it.

That being said, with jQuery you need only return false; and it handles everything for you