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 "ments" 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", "ments"));
});
});
})(jQuery.noConflict());
Thanks so much, I know this might be a pain to test but I'm really thankful.
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 "ments" 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", "ments"));
});
});
})(jQuery.noConflict());
Thanks so much, I know this might be a pain to test but I'm really thankful.
Share Improve this question asked Jun 13, 2010 at 22:38 Ben KulbertisBen Kulbertis 1,7134 gold badges18 silver badges30 bronze badges 8- Where are you placing this code in your page? And are you using any other frameworks? Does the other code execute? Have you tried minimizing it? – meder omuraliev Commented Jun 13, 2010 at 22:43
- This works for me, something else is going on outside of the posted code: jsfiddle/akgEH Also, do you need the noConflict? This gets real short if you don't. – Nick Craver Commented Jun 13, 2010 at 22:44
- @meder I have tried the code below the element that it is editing and also in the head. Neither have worked. Yes, other frameworks are in use but I shouldn't have issues since I'm using noConflict. Other jQuery code does execute without issue. No I haven't minimized it, would that help? @Nick That is interesting. I have tried it on a standalone html document where all there was was a link and the code in the head and it had the same results as on my site. Yes, unfortunately I do need the noConflict, I am using Wordpress and I am making use of plugins that require other frameworks. – Ben Kulbertis Commented Jun 13, 2010 at 22:54
-
Can you link us to said page with issue? Or replicate it on jsfiddle. It could be some Prototype extending code from another library which messes up how jQuery's
.each
or document ready code works, or event handlers may overwrite each other. – meder omuraliev Commented Jun 13, 2010 at 23:01 - Its on the homepage of this site: codezroz. The ment link under the latest post section is the issue. – Ben Kulbertis Commented Jun 13, 2010 at 23:04
3 Answers
Reset to default 5Your 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", "ments");
});
}).noConflict();
</script>
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", "ments"));
});
});
})(jQuery.noConflict());
Why not just:
jQuery(function($) {
$("a[href$='respond']").each(function() {
$(this).attr("href", $(this).attr('href').replace("respond", "ments"));
});
});
If you pass a function to the jQuery constructor it adds it to the DOM Ready listeners and automatically passes itself as an argument.