0

I am using latest Wordpress 3.6. I'd like to add some HTML code to the page using js. I put a

<script>  
        $('p.content').addClass('highlight'); 
     </script>

in the head are, but it doesn't work.

So I want to add the class highlight to a p element with calss=content.

the reason I want to use it, is to change the_content output in wordpress. I just want the text inside of ... I would also love to understand how I could change this with e.g. the functions.php... thanks.

  • 3
    What is child_add_scripts hooked to? – s_ha_dum Aug 10 '13 at 17:27
  • I used the code from the codex codex.wordpress.org/Function_Reference/… – ad2003 Aug 10 '13 at 19:55
  • That does not answer the question is a any clear way. – s_ha_dum Aug 10 '13 at 21:21
  • than sorry - but what do yoe mean exactly? child_add_scripts means to add the script to the child theme I am using... – ad2003 Aug 10 '13 at 22:22
  • 2
    Your function-- child_add_scripts-- is defined but is not used, at least not in the code posted. So what calls the function? What executes the function? – s_ha_dum Aug 10 '13 at 22:58
1

You cannot use the $ selector freely like this, because WordPress loads jquery in noconflict mode. The most common workaround is to wrap all functions in your script file:

jQuery(document).ready(function($) {  ... scripts ... }

If you want to put the script directly in the head of the file (not recommended) it would go like this:

<script>  
   jQuery('p.content').addClass('highlight'); 
</script>
|improve this answer|||||
1

In addition to the answer by cjbj, you can check your console for any errors. The shorthand $ isn't defined which means your JS will not work. Changing it to jQuery will work;

<script>  
    jQuery('p.content').addClass('highlight'); 
 </script>

Instead of

<script>  
    $('p.content').addClass('highlight'); 
 </script>

But as previously mentioned there are better places to add your JS.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.