SUBSCRIBE - [ Tech News ] [ Make Money Blogging Tips ] [ Online Marketing Tips ] [ Web Dev News ]
Powered by MaxBlogPress  

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.

image

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.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • LinkedIn
  • Reddit
  • StumbleUpon
  • Technorati
  • TwitThis
  • Yahoo! Buzz

Related posts brought to you by Yet Another Related Posts Plugin.

Comments

7 Responses to “NULL object errors using jQuery JavaScript library”
  1. rodkun says:

    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.

  2. MK says:

    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.

  3. Roman says:

    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!

  4. chris says:

    this was a big help! thanks

  5. brandon says:

    THANK YOU SO MUCH! I too was wondering what was going on with some code, and Google sent me to this helpful post.

  6. Tati says:

    Thanks for this post! I was stuck with the NULL object error.

  7. sarika says:

    Thanx buddi

    its very important point you told

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!