0

I have a bit of javascript that will dynamically add a static post to a loop. The script uses the jQuery 'after' method and on a static page it works beautifully but when inside the loop I don't get any results.

Can anyone see what i'm doing wrong here? Does WP not allow DOM insertion within a loop?

function newTile(){ var adTile = "<div class='new-box' id='article-tile'><h1> this is a test</h1> </div>"; var adLoc = 5; var tiles = $('#article-tile:nth-child('+adLoc+')'); $(tiles).after(adTile); } 
1
  • Are you sure that tiles contains a DOM element?
    – Naftali
    CommentedAug 27, 2012 at 14:30

1 Answer 1

1

WordPress requires no-conflict for jQuery.

Change this:

var tiles = $('#article-tile:nth-child('+adLoc+')'); $(tiles).after(adTile); 

...to this:

var tiles = jQuery('#article-tile:nth-child('+adLoc+')'); jQuery(tiles).after(adTile); 

Or else wrap your entire script accordingly:

jQuery(document).ready(function($) { // $() will work as an alias for jQuery() inside of this function }); 
1
  • 1
    Or even (function($){})(jQuery); would work ^_^
    – Naftali
    CommentedAug 27, 2012 at 14:33

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.