jQuery “'events' is null or not an object” error, when using empty() or html()

I am getting the following error in IE:

'events' is null or not an object -- jquery-latest.js?d=1848173663, line 113 character 467

I am using jQuery 1.4.2, I'm not in a position to upgrade yet as we are on an older version of jQuery UI and have way too many bugs using anything newer than 1.4.2.

I get following error when I run this bit of code the second time:

$.post("page/view.do?undoCache=" + Math.random(), {
   pageId: pId
}, function(xmlContent){
   console.log('1');//get this one
   $('#reloadCenterDiv').empty();
   console.log('2');//don't get this one unless line above is commented out, then will run til next line
   $('#reloadCenterDiv').html(xmlContent);
   console.log('3');//don't get this
});

I'm pretty sure I'm not doing anything else to #reloadCenterDiv between calls.

Googling around for the error "'events' is null or not an object" I found this:

"Sounds like a reference to an event handler is still there, when the handler itself is already gone."

That sounds logical. Any other ideas of why and when this error would occur?

I have found where this is happening, but all clues for me end there.

How do I clean things up so I can call empty() or html() on #reloadCenterDiv again?

Here is the HTML for #reloadCenterDiv:

<div id="reloadCenterDiv" style="border:none; margin: 0; overflow:auto; overflow-y:scroll; height: auto;"></div>

Answers


Not sure, but it would seem like jQuery.cache is being overwritten.

Since a DOM element has (when necessary) a serial number that maps to jQuery.cache, when you run a function like .empty(), jQuery assumes the related data exists, looks up the data for that element, and deletes it.

In place of your first log, do this:

console.log(jQuery.cache);

And see what it gives you. I'll bet that something is overwriting it. Perhaps you're loading jQuery twice?

Here's an example that intentionally deletes jQuery.cache. It gives a similar error.


EDIT:

Summary of the comments below. During .empty() (or actually cleanData()) jQuery grabs the expando from all descendant elements in order to delete the associated data.

The issue is that when jQuery does so, it assumes that the data was successfully located. In this case, somehow the data is being disassociated from the element, so retrieving the data using the value of the expando is returning undefined.

Because jQuery doesn't (or didn't in 1.4.2) verify that data was found, its attempt to access the events property on the data is causing an error, because again data is undefined.

Updated versions of jQuery fix it with if ( data && data.events ) {, which verifies that there is some object against which to ask for its events property.


Need Your Help

How to get the name and value of attributes from xml when using libxml2 sax parser?

iphone c objective-c xml libxml2

I got stuck on trying to detect the pair of name and value of the attributes in some general xmls by using libxml2 for parsing the api on iPhone application. For my project, the parsing speed is re...

Extracting text from PDFs in C#

c# pdf text extract

Pretty simply, I need to rip text out of multiple PDFs (quite a lot actually) in order to analyse the contents before sticking it in an SQL database.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.