I have a jQuery code that is supposed to insert some content after every paragraph on news articles. Like this:
$(document).ready(function() {
$("<a>The link inserted in jQuery</a>")
.insertAfter("p")
.attr("href", "http://bi.ng")
.addClass("abonnerreklame")
;
});
It works in the Fiddle that I've made, but not on my actual site. The reason behind that, have to be that the articles use dynamic PHP to render (WordPress):
<?php the_content(); ?>
Is there a way to get the JavaScript to work, even when the output is generated from PHP? I hope you can understand my question - please help if you have the knowledge to do so.
Here is my test site, by the way.
Thanks!
Just change $ by jQuery. it Will work. Check your console and you will see an error about '$'.
jQuery(document).ready(function() {
jQuery("<a>The link inserted in jQuery</a>")
.insertAfter("p")
.attr("href", "http://bi.ng")
.addClass("abonnerreklame")
;
});
This is because in the actual site, jQuery is in no-conflict mode, and it changes '$' with 'jQuery', because other frameworks (as Mootools) can use also '$'
Php is not dynamic. The content generated by a php script is static as far as the browser is concerned. When the content is served to the client, the client cannot tell the difference between content generated by a php script and static content. Since javascript only runs on the client, the same is true regarding javascript
From the link you provided, it looks like '$' is not defined properly or has been altered to suit wordpress noconflict.