1

I'm trying to load the comment form dynamically into their corresponding posts. What I have so far works for the first 6 posts that are loaded on the initial load. It's after the initial load that I'm running into problems. I need to be able to get this code to work on the remaining posts that are paginated and shown with an infinite scroll event. Any help would be greatly appreciated. Cheers, Mike

Commenform div in comment.php

<div id="commentform-<?php the_ID(); ?>" rel="<?php the_ID(); ?>" style="display: none"></div>

Comment form being called for in shortcodes.php

$output.='<div id="swap_comments-'.get_the_ID().'"rel="'.get_the_ID().'">'.comments_template('/comment.php').'</div>';

Comment form being handled in plugins.js

jQuery(window).ready(function() {
var commentscon = jQuery("div[id^='commentform']");
jQuery("div[id^='swap_comments']").append(commentscon);
jQuery.each(jQuery("div[id^='swap_comments']"),function(){
var CurrectDiv=jQuery(this);
CurrectDiv.find('div').filter(function(){
    if(jQuery(this).attr('rel')==CurrectDiv.attr('rel'))
    {
       jQuery(this).show(); 
    }
        });
});
});
user1813098
  • 25
  • 1
  • 7
  • see this [link](http://stackoverflow.com/questions/9272438/jquery-click-event-not-firing-on-ajax-loaded-html-elements) – rynhe Jan 31 '13 at 12:23

2 Answers2

1

Use jquery .on() which has replaced .live()

http://api.jquery.com/on/

Mandeep Jain
  • 2,304
  • 4
  • 22
  • 38
  • so i would write it out like this jQuery(window).on(function()? – user1813098 Jan 31 '13 at 12:10
  • No, use it like this jQuery("div[id^='swap_comments']").on(function(){}); – Mandeep Jain Jan 31 '13 at 12:14
  • Hmm...just tried that and now none of the comment forms are showing up..thoughts? – user1813098 Jan 31 '13 at 12:20
  • can you please write your code under a single $(window).ready(function(){})? and write the code for appending after the .on event handler ? – Mandeep Jain Jan 31 '13 at 12:46
  • Hi there..first off i'm not terribly familiar with jQuery thus the request for help..secondly i've edited my question and hopefully that's what you are asking for. Regards – user1813098 Jan 31 '13 at 12:54
  • Nope. I was asking to merge your code frome two jQuery(window).ready(function() {}) into one, since the window only loads once, hence no point writing two different functions. Secondly, what i was trying to tell is instead of using jquery.each for finding all "div[id^='swap_comments']", you could simply attach the .on() event handler on to it, so that every time it loads, the function would execute – Mandeep Jain Jan 31 '13 at 13:04
  • okay..i took care of that in the question. also when i made the changes you recommended the forms disappear. – user1813098 Jan 31 '13 at 13:56
0

You have to use live function to assign function to dynamicly added elements.

david.binda
  • 1,058
  • 9
  • 15