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.
12 Responses
Hi MK,
I am experiencing the same error but it only happen when I am using Selenium to test the web page. Is there other solution for this? I tried replacing the “$” with “jQuery” but it still display the “‘null’ is null or not an object” error.
Hi There,
Yes, there are other options for JQuery conflicts with other libraries and can be found here – http://docs.jquery.com/Using_jQuery_with_Other_Libraries
Please let me know if this doesnt help and I will look inot this for you, Thanks.
Hi, I actually wasn’t suffering a NULL object error but found your simple JQuery fix for the ‘$’ problem worked great on a basic library conflict I was encountering.
After hours of reading crap tutorials and forum posts and trying pages and pages of code (none of which worked) I stumbled upon your post on Google. My problem was fixed in 2 minutes!!!
Thank you very much for saving my sanity!
this was a big help! thanks
THANK YOU SO MUCH! I too was wondering what was going on with some code, and Google sent me to this helpful post.
Thanks for this post! I was stuck with the NULL object error.
Thanx buddi
its very important point you told
Thank you MK
Thank you so much, MK!
I’ve got two similar sites, using the same jQuery scripts. One was working fine, and the other was throwing the Null error and refusing to cooperate. After a few hours of trying to track down the culprit, I came across your post, swapped out the $ with jQuery in the script, and, voila! It’s working like a champ.
I really appreciate you sharing this info with everyone. You saved me a lot of time and trouble.
Patrick
It’s Works!!..Thanks!!
Thanks for posting this. It was a quick reminder that prevented me from wasting a bunch of time. Thanks-
thank you , it helps me very much