jQuery code not executing when DOM is ready

Posted by Ben on Stack Overflow See other posts from Stack Overflow or by Ben
Published on 2010-06-13T17:56:52Z Indexed on 2010/06/13 18:02 UTC
Read the original article Hit count: 207

I have written a simple jQuery script that changes the hash tag of a link. I do this because I am using IntenseDebate comments in Wordpress but the comment link still links replaced id of the old comments. I'm using jQuery so that if javascript is enabled, the user can click on the comments link and it will take them to the IntenseDebate comments. If its not enabled it will take them to the traditional comments (because IntenseDebate requires javascript to function).

The issue I'm having lies in this script to change the hash tag at the end of the link. Currently the URL "someurl.com/#respond" but I need the script to change it to "someurl.com/#comments". What is happening is that the script doesn't work, however I believe my syntax is correct so I decided to try copying and pasting the code into Firebug's console and the code executed perfectly. I could see that the link I was trying to change was now correct, and I could click on it and it worked as I desired. So what I don't understand is why the code is not executing when it is supposed to. I am using $(document).ready() and I have other jQuery on the page that executes just fine. I even tested it on a simple HTML page away from all the problems that might be caused by Wordpress and I received the same result. Does anyone know why this might be happening?

Here is my code (I am using noConflict because Wordpress makes use of other frameworks):

jQuery.noConflict();
jQuery(document).ready(function($) {
$("a[href$='respond']").each(function() { 
    this.href = this.href.replace("respond", "comments");
});
})(jQuery);

Thanks very much for your help!

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery