NULL object errors using jQuery JavaScript library
January 18, 2009 by MK
Filed under Blogging, Wordpress, web development
While using jQuery JavaScript library with other JavaScript libraries e.g. scriptaculous or dojo etc. you might experience NULL object errors.
I was using Dynamic JavaScript Ad Rotator Slideshow script with my WordPress BLOG and WordPress theme I am using uses jQuery. I was getting “null” is null or not an object error whenever I was enabling my Dynamic Ad Rotator script. Here is a screen shot of the error.
And here is the code throwing the error -
jQuery().ready(function(){
$(’#pingbacks’).hide();
$(’#pingback’).click(function(){
$(this).siblings(’#pingbacks’).slideToggle(’slow’);
});
});
Most of the times the error was just because of the conflict, jQuery uses $ as a shortcut for “jQuery”. You can override that default by calling jQuery.noConflict() at any point after jQuery and the other library have both loaded. For example:
When you use more than one libraries with jQuery which use $ sign for selection of code block, use following script.
jQuery.noConflict();
// instead of $ use jQuery as
jQuery(document).ready(
function(){
});
This will remove the conflict between different libraries.
So the final code I ended up using is -
jQuery.noConflict();
jQuery().ready(function(){
jQuery(’#pingbacks’).hide();
jQuery(’#pingback’).click(function(){
jQuery(this).siblings(’#pingbacks’).slideToggle(’slow’);
});
});
And you dont need to use jQuery.noConflict(); if you are replacing $ with jQuery. That will be done by the script automatically.














