SUBSCRIBE - [ Tech News ] [ Make Money Blogging Tips ] [ Online Marketing Tips ] [ Web Dev News ]
Powered by MaxBlogPress  
CoolWebDeveloper.com CoolWebDeveloper.com
Menu
Search
NULL object errors using jQuery JavaScript library

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.

arrow12 Responses

  1. 49 mos, 4 wks ago

    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. 49 mos, 3 wks ago

    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. 41 mos, 2 wks ago

    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
    39 mos, 3 wks ago

    this was a big help! thanks

  5. brandon
    38 mos, 2 wks ago

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

  6. 38 mos ago

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

  7. sarika
    37 mos, 3 wks ago

    Thanx buddi

    its very important point you told

  8. Velmurugan S
    30 mos, 2 wks ago

    Thank you MK

  9. Patrick Villella
    27 mos, 3 wks ago

    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

  10. 21 mos ago

    It’s Works!!..Thanks!!

  11. 19 mos, 3 wks ago

    Thanks for posting this. It was a quick reminder that prevented me from wasting a bunch of time. Thanks-

  12. sherif sakr
    16 mos, 1 wk ago

    thank you , it helps me very much

Leave A Comment

CommentLuv badge