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);
}
  • Are you sure that tiles contains a DOM element? – Neal Aug 27 '12 at 14:30
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
    Or even (function($){})(jQuery); would work ^_^ – Neal Aug 27 '12 at 14:33

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.