3

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

<script>
    $('a[href="http://domain1"]').attr("href","http://domain2");
</script>
4
  • any errors in console Commented Feb 25, 2014 at 8:10
  • 2
    "JQuery doesn't work on WordPress" Yes, it does. Commented Feb 25, 2014 at 8:11
  • Be sure you haven´t included it twice. Check your sourcecode. Commented Feb 25, 2014 at 8:11
  • @MilindAnantwar No errors in the console Commented Feb 25, 2014 at 8:21

2 Answers 2

5

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>
Sign up to request clarification or add additional context in comments.

5 Comments

"...and eliminate new line characters and unnecessary spaces..." Why? How can that be even remotely necessary?
@T.J.Crowder It worked perfectly only when all the newline characters are eliminated..
@user3206125: I find that very, very hard to believe. I suspect observational error.
@T.J.Crowder I know but that was the truth,
@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.
4

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); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.