Keith
Keith

Reputation: 155702

IE7 & 8 not fireing jQuery click events for elements appended inside a table

I have an IE bug that I'm not sure how to fix.

Using jQuery I'm dynamically moving a menu to appear on an element on mouseover.

My code (simplified) looks something like this:

$j = jQuery.noConflict();

$j(document).ready(function()
{
    //do something on the menu clicks
    $j('div.ico').click(function() { alert($j(this).parent().html()); });

    setUpActions('#tableId', '#menuId');
});

//on mouseover set up the actions menu to appear on mouseover
function setUpActions(tableSelector, menuSelector)
{
    $j(tableSelector + ' div.test').mouseover(function()
    {
        //note that append will move the underlying
        //DOM element with all events from it's old
        //parent to the end of this one.
        $j(this).append($j(menuSelector).show());
    });
}

This menu doesn't seem to register events correctly for the menu after it's been moved in IE7, IE8 and IE8-as-IE7 (yeah MS, that's really a 'new rendering engine' in IE8, we all believe you).

It works as expected in everything else.

You can see the behaviour in a basic demo here.

In the demo you can see two examples of the issue:

  1. The image behind the buttons should change on hover (done with a CSS :hover selector). It works on the first mouseover but then persists.
  2. The click event doesn’t fire – however with the dev tools you can manually call it and it is still subscribed.

You can see (2) in IE8's dev tools:

  1. Open page in IE8
  2. Open dev tools
  3. Select "Script" tab and "Console" sub-tab
  4. Type: $j('#testFloat div.ico:first').click() to manually call any subscribed events
  5. There will be an alert on the page

This means that I'm not losing the event subscriptions, they're still there, IE's just not calling them when I click.

Does anyone know why this bug occurs (other than just because of IE's venerable engine)?

Is there a workaround?

Could it be something that I'm doing wrong that just happens to work as expected in everything else?

Upvotes: 13

Views: 26656

Answers (4)

altschuler
altschuler

Reputation: 3922

I had the same problem, but none of the previous solutions worked. The problem turned out to be that IE7 (I haven't tested other versions of IE) will not register the .click if it's chained after an animate function. So this will NOT work:

$("div.menubar-item").animate({
    color:"#000"
}, 0)

.click(function()
{
    //not firing
})

But this WILL work

$("div.menubar-item").click(function()
{
    //firing!
})

$("div.menubar-item").animate({
    color:"#000"
}, 0)

EDIT: The same is true for mouseenter, mouseleave, etc.

EDIT 2: It will still fire events and animate if you chain the animate function AFTER the click, so this is "valid" too:

$("div.menubar-item").click(function()
{
    //firing!
})

.animate({
    color:"#000"
}, 0)

Upvotes: 2

Eifion
Eifion

Reputation: 5553

Strangely, although your click event isn't firing in IE, if you change it to either mousedown or mouse up it works as you'd expect although you still have your image hover issue.

$j('div.ico').mouseup(function() { alert($j(this).parent().html()); });

Upvotes: 10

Chad Grant
Chad Grant

Reputation: 45382

IE doesn't copy events to items dynamically appended to the DOM.

Try binding your click event after you have added what you need to be bound or use .live()

if you are using clone() remember to pass clone(true) to explicitly request copying of event handlers.

var actionMenu = $j(menuSelector);

//actionMenu is now an Instance of jQuery, not the DOM object
//because jQuery is chainable

actionMenu.hide();

// notice the clone(true)

$j(this).append( actionMenu.clone(true) );

jQuery is chainable, so you can also write this in "jQuery" syntax as:

var clone = $j(menuSelector)
               .clone(true)
               .css('background-color', $j(this).css('background-color') );

$j(this).append(clone);

I don't think you need the show/hide since it happens so fast.

Upvotes: 1

Richard
Richard

Reputation: 22016

I would highly recommend using the Live events instead from the latest jquery.

this way you can bind to elements by css class which have not been created at the beginning but the click binding will be added when the new elements are added:

http://docs.jquery.com/Events/live

Upvotes: 0

Related Questions