0

trying to incorporate a jQuery script into a Wordpress site.

I created a custom page in my theme and added the following in the body tag:

<body>
...
<div id="msgid"> hello there
</div>
...
</body>

I also created a test.js file with the following:

$(document).ready(function(){
 $("#msgid").html("Hello world.");
});

I am loading the test.js in the footer.

I was expecting to see Hello world. in the div. But it doesn't show anything.

Any ideas?

Thanks.

1 Answer 1

5

Wordpress by default uses the noConflict version of jQuery, which means it doesn't load jQuery to the $ sign.

You can do this:

jQuery(document).ready(function($){
   //inside of here you passed in the $ sign to be used as an alias of jQuery
   $("#msgid").html("Hello world.");
});

See here for more details on jQuery's noConflict function: http://api.jquery.com/jQuery.noConflict/

Sign up to request clarification or add additional context in comments.

2 Comments

thanks. Do I have to use function($) or can I use function() and use jQuery("#msgid") instead of $("#msgid")? Thanks.
@jdias You can use jQuery("#msgid") as well. The important thing to understand here is that by passing in the $ into function, you're essentially assigning jQuery to $. So imagine this is happening: $ = jQuery.

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.