Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery doesn't work on WordPress

I'm trying to use JQuery in WordPress pages but it doesn't work:

<script>
    $('a[href="http://domain1"]').attr("href","http://domain2");
</script>
like image 389
Dexpras Avatar asked Dec 01 '25 20:12

Dexpras


2 Answers

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>
like image 57
Mark Timothy Avatar answered Dec 03 '25 13:12

Mark Timothy


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); 
like image 36
Felix Avatar answered Dec 03 '25 14:12

Felix