0

I am using the WP-Polls plugin for WordPress which is an AJAX polling system and I'm trying to append an initially hidden div (.comment-block) to a new div (.voted) which will be dynamically inserted into the DOM by the WP-Polls plugin upon AJAX success. Once the user has voted and clicked on the vote button (.Buttons), the DOM updates to reflect the addition of the new (empty) div with a class of .voted. I put up a similar question earlier but thought I'd open a new more relevant thread.

Note: The .voted div was created through the templates that WP-Polls offers in the dashboard. The plugin offers templates for "before the user has voted" and "after the user has voted" and what I did was insert a div into the latter like this: <div class="voted"></div>. The reason why I can't just add content inside that div directly is because the content in the div to be appended (.comment-block) is a contact form created by the plugin Contact Form 7 and it requires a PHP statement. Only HTML is allowed in the templates.

Among other various failed attempts, I have tried to use .on so that clicking on .Buttons would activate the function. However, nothing was changed in the DOM.

$(document).on('click', '.Buttons', function() {
    $('.comment-block').appendTo('.voted');
});

Below is the HTML. This is before the user has voted:

<div id="poll">
    (poll here) + .Buttons vote button                  <-- in here----------| 
</div>                                                                       |
<div class="comment-block" style="display:none">        <-- I want this div  |
    <?php echo do_shortcode('[contact-form-7]'); ?>
</div>  

And this is how I want it to look after the user has voted:

<div id="poll">
    <div class="voted">                                 <-- dynamically created div
        <div class="comment-block" style="display:block">
            <?php echo do_shortcode('[contact-form-7]'); ?>
        </div> 
    </div>
</div>                                              

If anyone can show me the way, I'd appreciate it. I've been racking my brain with this one for hours.

Edit: I am not up to speed with AJAX so I'm unable to provide exactly the code that is needed, but here is a list of the files for the plugin: https://github.com/lesterchan/wp-polls

J82
  • 8,267
  • 23
  • 58
  • 87
  • You're going to have to show the ajax code if you want a more "precision-guided" (that is, tailored to your situation) response. Probably you already have an excellent grasp of AJAX, but if a refresher would help [here are a few examples](http://stackoverflow.com/questions/17973386/ajax-request-callback-using-jquery/17974843#17974843) – cssyphus Sep 10 '14 at 00:54
  • @gibberish You know, I thought it'd be a good idea to include that but I wasn't sure which file to show. I actually don't have a good grasp of AJAX. Here are the list of files, though, if you'd care to take a look. I believe `wp-polls.php` is the one you're requesting: https://github.com/lesterchan/wp-polls – J82 Sep 10 '14 at 01:09

3 Answers3

0
$('button.Buttons').click(function ()
{
    $('#poll').empty().append('<div class="voted"></div>');
    $('.comment-block').show().appendTo('.voted');
    $(this).unbind('click');
});

You can also change the '.show()' to actually set the style specifically to display:block if you need to.

  • Can you explain the purpose of this line: `$('#poll').empty().append('
    ');` The `.voted` div would already be dynamically inserted by the plugin once the user has voted. I'm just trying to understand what that line is for.
    – J82 Sep 10 '14 at 00:08
  • 1
    @J82 `.empty()` deletes everything in the `#poll` div. `.append()` then adds the `.voted` DIV *to the end* (that is, as the last stuff in) the newly emptied `#poll` div. Since the div is empty, you could use either `append()` or `prepend()` or `.html()` – cssyphus Sep 10 '14 at 00:50
0

If I understand your question correctly, this should work:

$('.Buttons').on('click', function () {
  setTimeout(function () {
    var commentBlock = $('.comment-block'),
      cloneComment = commentBlock.clone();
      commentBlock.remove();
    $('#poll').append(cloneComment);
    cloneComment.wrap('<div class="voted">').show();
  }, 1000);
});

First, bind the 'click' to the '.Buttons' element. Then, create a clone of the '.comment-block' element so you can .remove() the original '.comment-block' and .append() or .prepend() the cloned element to the '#poll' element. Lastly, .wrap() the clone with a <div class="voted"> and call .show() to display the clone.

Brett
  • 3
  • 6
  • I can see the logic behind this but for some reason, when I click `.Buttons`, the `.comment-block` div just disappears. I looked through the DOM to see if it was wrapped and appended to `#poll` but it's nowhere to be found. Also, the `.voted` div is already dynamically inserted by the plugin so is there a need to wrap it? Or I guess I can just take that div out of the poll template in the dashboard. – J82 Sep 10 '14 at 00:32
  • It looks like when `.Buttons` is clicked, `.comment-block` shows up briefly, then fades away with the rest of the voting form. So I'm assuming the `.comment-block` div is being appended too early before the AJAX action when it should be appended after the user has voted. Just letting you know just in case the new info helps out in any way. – J82 Sep 10 '14 at 05:12
  • @J82 sounds like the plugin is manipulating the DOM elements you are trying to reference (#poll, .voted, etc). A few options for you to pursue: If the plugin has documentation, check if there is a callback defined; or you may need to find a way to manipulate static DOM elements instead... – Brett Sep 10 '14 at 17:08
  • I looked through the documentation but there doesn't seem to be any sort of callback. Is there any way to delay this code just a bit until after the AJAX has successfully finished? It looks like that would do it because right now I can see the comment form getting wrapped up with `.voted` but it disappears right away. – J82 Sep 10 '14 at 18:07
  • See updated code: This is not generally recommended because using a set time to delay a callback is extremely arbitrary. Too many variables determine how long it will take a function to execute before a callback can run. So you really can't pin it down to a time. However, setTimeout() will delay the execution of a function for a specified time in milliseconds (in the code near the bottom, 1000 = 1sec). So the updated code will run 1 second after .Buttons is clicked. Not sure if this will work. You can also try increasing the time if necessary. – Brett Sep 10 '14 at 21:25
0

You need to trigger an event from the ajax call:

The button onclick function fires the ajax call. The ajax success fires event(s) on the targeted elements to be changed and passes it's response as a parameter.

example: html

<div id="foo">bar</div>
<button onclick="ajaxCall();" />

javascript

var ajaxCall = function() {
  $.ajax({
    success: function(response) {
      $("#foo").trigger("ajsuccess", response);
    }
  });
};
$("#foo").on({
  "ajsuccess" : function(e, response) {
    $(this).append(response);
  }
});
Chris Caviness
  • 586
  • 3
  • 10