3

I am working in wordpress where i have event panel where i have upcoming event detail and i am showing that how much time is left for next or upcoming event. I am getting values dynamically from database and i am managing the time left for upcoming event by the date given by user in admin panel. Now the time is coming up properly but the problem is that its showing one time for all upcoming events means repeating one time for all events.

Here is the jquery script:

$(document).ready(function(){
   var calculate=$('.thematchdate').val();
   $('.counter').countdown(calculate, function(event) {
       $(this).html(event.strftime(''
      + '<span>%D <br> <small>days</small></span>  '
      + '<span>%H <br> <small>hr</small> </span>  '
      + '<span>%M <br> <small>min</small> </span>  '
      + '<span>%S <br> <small>sec</small></span> '));
   });
});

I also tried like that !.

var calculate="";
$('.thematchdate').each(function(index) {
   calculate=$(this).val();

   $('.event-ones').each(function(index){
      alert(calculate+''+index);
      $(this).countdown(calculate, function(event) {
        $(this).html(event.strftime(''
          + '<span>%D <br> <small>days</small></span>  '
          + '<span>%H <br> <small>hr</small> </span>  '
          + '<span>%M <br> <small>min</small> </span>  '
          + '<span>%S <br> <small>sec</small></span> '));
      });
   });
});

Here is the Mark up from which i am taking up value and returning time left:

<input type="hidden" class="thematchdate" value="<?=get_post_meta($p, '_start_date', true); ?>">
<div id="event-one" class="counter event-ones"></div>

Here is the full mark up of that panel

 //loop over each post
                    foreach($posts as $p){
                       ?>
                                    <!-- Item  Event  -->
                                    <li>
                                        <div class="content-counter">
                                            <p class="text-center"> 
                                                <i class="fa fa-clock-o"></i> 
                                                Countdown Till Start
                                            </p>
                 <input type="hidden" class="thematchdate" value="<?=get_post_meta($p, '_start_date', true); ?>">
                    <div id="event-one" class="counter event-ones"></div>
                                            <ul class="post-options">
                                                <li><i class="fa fa-calendar"></i>June 12, 2014</li>
                                                <li><i class="fa fa-clock-o"></i>Kick-of, 12:00 PM</li> 
                                            </ul>
                                            <ul class="list-diary">
                                                <!-- Item List Diary --> 
                                                <li>
                                                    <h5><?=get_post_meta($p,'_league_title',true); ?><span><?=get_post_meta($p, '_start_date', true); ?>- 11:00</span></h5>
                                                    <ul class="club-logo">
                                                        <li>
 <img style="width:100px; height:89px;" class="img img-circle img-responsive" src="<?=get_post_meta($p , '_boj_mbe_image1', true); ?>" alt=""> 
<h6><?=get_post_meta($p, '_team_name1', true); ?></h6>
                                                        </li>
                                                        <li><span>Vs</span></li>
                                                        <li>
  <img style="width:100px; height:89px;" class="img img-circle img-responsive" src="<?=get_post_meta($p , '_boj_mbe_image2', true); ?>" alt="">
 <h6><?=get_post_meta($p, '_team_name2', true);?></h6>
                                                        </li>
                                                    </ul>
                                                </li>
                                                <!-- End Item List Diary --> 
                                            </ul>
                                            <a class="btn btn-primary" href="http://localhost/football/events/">
                                                VIEW EVENT PAGE
                                                <i class="fa fa-trophy"></i>
                                            </a>
                                        </div>
                                    </li>
                                    <? } ?> 
falsarella
  • 12,217
  • 9
  • 69
  • 115
Vicky
  • 982
  • 2
  • 11
  • 28
  • 1
    Get rid of that hidden input fields and print the date in your div `data` attribute - http://jsfiddle.net/bnju82uj/ . Also if you need the `id="event-one"` on your divs, at least make sure it's unique for each div. – Artur Filipiak Mar 12 '15 at 20:31

1 Answers1

4

Your first attempt wasn't iterating each event to calculate it separately, so all events got the same calculated time.

Your second attempt was iterating each event and calculating it separately, but for each calculated value it was printing it to all events, so the last calculated value was being shown to all events.

So, just change your second attempt to print the calculated time only to its corresponding element, like this:

var calculate="";
$('.thematchdate').each(function(index) {
    calculate=$(this).val();

    $(this).next('.event-ones').countdown(calculate, function(event) {
        $(this).html(event.strftime(''
            + '<span>%D <br> <small>days</small></span>  '
            + '<span>%H <br> <small>hr</small> </span>  '
            + '<span>%M <br> <small>min</small> </span>  '
            + '<span>%S <br> <small>sec</small></span> ')
        );
    });
});

In this example, I've used jQuery's .next to get the following .event-ones sibling element and call the countdown widget to it, since it's iterating through each .thematchdate element, as the markup you demonstrated:

Here is the Mark up from which i am taking up value and returning time left:

<input type="hidden" class="thematchdate" value="<?=get_post_meta($p, '_start_date', true); ?>">
<div id="event-one" class="counter event-ones"></div>
falsarella
  • 12,217
  • 9
  • 69
  • 115
  • let me check and it seems to be working because you have added .next function i didnt knew about that one – Vicky Mar 12 '15 at 20:14
  • yup its working but will you please elaborate the next function what does it really do please improve your answer i will be very thankfull to you – Vicky Mar 12 '15 at 20:16
  • @Vicky Glad it worked! I have updated my answer accordingly. I hope you understood the concepts and suggest to read the jQuery documentation to have a more powerful resource knowledge! – falsarella Mar 12 '15 at 20:25
  • 1
    sincerely thank you i have read .next() official documentation as well as traversing and all the events which is related with it and you made my day really learn full day a last – Vicky Mar 12 '15 at 20:37