I'm trying to use JQuery in WordPress pages but it doesn't work:
<script>
$('a[href="http://domain1"]').attr("href","http://domain2");
</script>
Mihai Iorga
39.8k17 gold badges109 silver badges109 bronze badges
-
any errors in consoleMilind Anantwar– Milind Anantwar2014年02月25日 08:10:48 +00:00Commented Feb 25, 2014 at 8:10
-
2"JQuery doesn't work on WordPress" Yes, it does.T.J. Crowder– T.J. Crowder2014年02月25日 08:11:06 +00:00Commented Feb 25, 2014 at 8:11
-
Be sure you haven´t included it twice. Check your sourcecode.Top Questions– Top Questions2014年02月25日 08:11:46 +00:00Commented Feb 25, 2014 at 8:11
-
@MilindAnantwar No errors in the consoleDexpras– Dexpras2014年02月25日 08:21:42 +00:00Commented Feb 25, 2014 at 8:21
2 Answers 2
You must use jQuery keyword instead of $ shortcut when you put JQuery in WordPress pages..
and eliminate new line characters and unnecessary spaces as well like shown below:
<script>jQuery(document).ready(function($){$('a[href="http://domain1"]').attr("href","domain2");});</script>
answered Feb 25, 2014 at 8:11
Mark Timothy
1,8245 gold badges21 silver badges30 bronze badges
Sign up to request clarification or add additional context in comments.
5 Comments
T.J. Crowder
"...and eliminate new line characters and unnecessary spaces..." Why? How can that be even remotely necessary?
Dexpras
@T.J.Crowder It worked perfectly only when all the newline characters are eliminated..
T.J. Crowder
@user3206125: I find that very, very hard to believe. I suspect observational error.
Dexpras
@T.J.Crowder I know but that was the truth,
Mark Timothy
@T.J.Crowder When it comes to any theme php file, this is not necessary but when adding JQuery through the dashboard->pages. please note that this is not happening always.
Normally jQuery is conflict with Wordpress since they're both using $, try to do:
jQuery(document).ready(function($) {
$('a[href="http://domain1"]').attr("href","http://domain2");
});
or put your code inside a closure:
(function($){
$('a[href="http://domain1"]').attr("href","http://domain2");
})(jQuery);
answered Feb 25, 2014 at 8:10
Felix
38.2k8 gold badges46 silver badges56 bronze badges
Comments
lang-js