Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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);
}
share|improve this question
    
Are you sure that tiles contains a DOM element? –  Neal Aug 27 '12 at 14:30

1 Answer 1

up vote 1 down vote accepted

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
});
share|improve this answer
1  
Or even (function($){})(jQuery); would work ^_^ –  Neal Aug 27 '12 at 14:33
    
That did it, thanks! –  CNCPTS Aug 27 '12 at 14:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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