views:

87

answers:

3

I'm having quite the brainbuster of an issue right now. I am trying to change a link's hash tag at the end from "respond" to "comments" with jQuery. I have a simple script that should do this, however it does not work. The link does not change. However, Firebug shows no errors and when I run the code in Firebug's console, it works just as I intend. Why doesn't this work on its own? Does anybody have a solution, I'm at my wits edge with this one.

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

Thanks so much, I know this might be a pain to test but I'm really thankful.

+4  A: 

You're using $(document).load() instead of $(document).ready().

(function ($) {
//---------------v
    $(document).load(function() {    
        $("a[href$='respond']").each(function() {
            $(this).attr("href", $(this).attr('href').replace("respond", "comments"));
        });  
    });
})(jQuery.noConflict());
patrick dw
Sorry, I put that there to see if it would make any difference and never changed it back. I just did, it doesn't help.
Ben
@Ben - You have lots of other scripts. Not sure what they all do, but it concerns me that your script is in the middle of the page, embedded in a `div`. Perhaps another script is manipulating that element and killing your code. Could you move your script to the `head` or the bottom of the page and try that?
patrick dw
@patrick - I have tried that before, but I suppose there is no harm in trying it again. But as I said before, I tried this on a simple page that had nothing but the link and the code in the head and it produced the same results.
Ben
@Ben - Any chance that link is being generated dynamically via some other javascript? If so, maybe it isn't on the page yet when your script runs.
patrick dw
@patrick - Its being generated by PHP, but that should be served before the page loads, am I correct? I have also tried applying a delay, just in case something like this was causing it, however it didn't help.
Ben
@Ben - Yeah, shouldn't matter if it is via PHP. Thought maybe some other javascript did it, but if you used a `setTimeout()`, then that's probably not the case. Just for kicks, try using the `contains` selector instead of the `ends with`: `a[href*='respond']`. Your code works fine on a test page. There's something else going on. I'll be back in about an hour.
patrick dw
@patrick - Take a look at this. http://codezroz.com/test.html This is the test page I referred to. Its just the code and an identical link, yet it does the same thing. Which is why I am led to believe it is an error in the code, yet I can find none.
Ben
@Ben - ...one more thing. Does *any* jQuery work for you? Try this in your `ready()` method: `alert($().jquery);` Should alert the version of jQuery you're using.
patrick dw
@patrick - Thanks man, I appreciate all the help. The contains selector didn't make a difference.
Ben
@Ben - I literally cut and pasted the code from your test page, as well as the `<a>` element. Works fine for me. I'll be back in a bit.
patrick dw
@Ben - The problem with your test page is that the opening javascript tag is misspelled `javscript`. Be back.
patrick dw
@patrick - Sure, take your time. Although I may have just found something. It seems none of that code is executing. When I paste the alert code into the ready() method and reload the page no alert box is shown. But I do have other jQuery code running on the page that works just fine. At least this narrows it down a bit.
Ben
@patrick The misspelling of javascript was the whole problem. Thanks so much for your help patrick. I'm sorry that the issue was just a typo. Thanks so much for all your help.
Ben
@Ben - Seemed like it was spelled correctly on the main page, though. Well anyway, not a problem. :o) Glad things are working for you now.
patrick dw
+4  A: 

Hi Ben,

Your code should be working fine, but your script tag is malformed. You have text/javscript instead of text/javascript. Also, you can optimize your code a little:

<script type="text/javascript">
  jQuery(document).ready(function($){
    $("a[href$='respond']").attr("href", function(index, attr){
      return attr.replace("respond", "comments");
    });
  }).noConflict();
</script>
Doug Neiner
Wow, don't I feel dumb now...and really embarrassed. Thanks Doug. After struggling with this for 8 hours...it was because I typo'd javascript. Thanks for the help, and for the optimized code!
Ben
Not answer-crapping but shouldn't you select the person who's been helping you for the last hour?
meder
Happy to help! I'll tell you a secret: @Jonathan Sampson (Moderator here on SO) shot me a bit of code he was troubleshooting a few months ago, and he couldn't find the error. He had `<script type="text/css">` haha. So you aren't alone :) Good luck on your site!
Doug Neiner
@meder - Shouldn't I actually mark the person who found the answer? And I apologized to patrick and am sincerely sorry for my mistake and am 100% grateful for his help.
Ben
@Ben ` The problem with your test page is that the opening javascript tag is misspelled javscript. Be back. – patrick 13 mins ago` whereas this was answered 10 mins ago.
meder
@meder - My goodness, I never saw that. Sorry Doug but I had to switch the answer. I'm sure you understand. Thanks meder for pointing that out, I don't want to hurt anyone's feelings.@Doug - I don't believe you ripped off the answer and am very grateful for the rewritten code.
Ben
@Doug - I don't think you ripped off his answer, I think both of you solved it independently. It's just I'm pointing out that he found it before yours, and even though you found it faster than he did, that doesn't take away from his whole time helping debug. Truthfully both of you should be selected.
meder
@Ben no worries, I don't need the rep points, just happy you got it figured out!
Doug Neiner
Well, sorry for the trouble then.
meder
@Doug - Thanks for understand Doug. I really appreciate it!@meder - Thanks for trying to help patrick out. He deserved it.
Ben
@meder You of course make a totally valid point. Sorry for getting upset and railing on you :( I deleted most of my comments, so I look like less of a jerk :)
Doug Neiner
@Doug - +1 Looks like there was a bit of a storm here while I was gone. ;o) Honestly, I'd have been happy to have you get the credit just to see this one figured out when I returned. It was starting to driving me a bit crazy. :o)
patrick dw
@meder - Thanks for your efforts. I appreciate it. :o)
patrick dw
@patrick Haha... Ya, I know better but I argued anyway :) Silly me. Thanks for the nice comment!
Doug Neiner
A: 

Why not just:

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

If you pass a function to the jQuery constructor it adds it to the DOM Ready listeners and automatically passes itself as an argument.

Sebastián Grignoli