Search code examples
javascriptjqueryasp.netclickinternet-explorer-11

ASP.NET JQuery .click() not working in IE11


The following .click()-method is fired in Chrome without problems. In Internet Explorer it is only fired if I refresh the page (F5). If I access the page by entering the url (or redirected by a buttonclick from an other page) the .click-method is NOT fired.

But if I put an alert("?") before the .click() it works in IE too!

Why does it not work correctly in IE? I can't let the alert() be there...

$(window).load(function() {
    //Fake click on the last used Tab
    alert("?");
    $("#"+GetCookie("lastTab["+window.location.pathname+"]")).click();
});

=> The available (clickable) tabs are created in

jQuery(document).ready(function($) {    
...
});

EDIT From comments:

They are created inside the .read(function($) in this way:

$("#HillbillyTabs").append('<li><a href="#Tab' + upperIndex + '" id="TabHead' + upperIndex + '" onclick="SetCookie(this.id);">' + title + '</a></li>').after('<div id="Tab' + upperIndex + '"></div>');

After Container is created after the script:

<div id="tabsContainer"><ul id="HillbillyTabs"></ul></div>

Solution

  • Do not try to inject the function call, but rather add an event listener to the code. For example: (I made up some variables as your code did not indicate some things here)

    var upperIndex = $('#tabsContainer').find('ul').length;
    var title = "mytitle";
    var newHeadId = 'TabHead' + upperIndex;
    var newTabId = 'Tab' + upperIndex;
    
    $("#HillbillyTabs").append('<li><a href="#' + newTabId + '" id="' + newHeadId + '">' + title + '</a></li>').after('<div id="' + newTabId + '"></div>');
    
    $("#HillbillyTabs").on('click', '#' + newHeadId, function() {
      console.log(this.id);
      SetCookie(this.id);
    });