Avatar of Jon Imms
Jon ImmsFlag for United States of America

asked on 

show all posts on page load - jQuery dropdown.

Hi there,  
I have a page which will show past and future webinars (my CPT).
For this i have a dropdown, which will show the contents of a div associated with the value.
All is working, but on page load, the content is empty, just shows the dropdown as Topic.
If i select Business Management, those post will then show.
If it then select Topic, all posts show.

How can i modify my jQuery function, so that it displays "all-future" on page load. ? 


Part of php template
<div class="blog-filters">
            <div class="select-container">
                <select id="selectMe">
                    <option value="all-future">Topic</option>
                    <option value="business-management">Business management</option>
                    <option value="financials">Financials</option>
                    <option value="project-management">Project management</option>
                    <option value="sales-marketing">Sales and Marketing</option>
                    <option value="virtual-training">Virtual Training</option>
                </select>
            </div>
        </div> 

<div class="wp-block-group alignfull">
    <div class="wp-block-group__inner-container">

    <div class="wp-block-group tabbed-content all-future">
            <div class="wp-block-group__inner-container">

            <?php
                // All future webinars
                // get today's date
                $today = date( 'Y-m-d' );
 
                // get posts
                $futureposts_all = get_posts(array(
                    'post_type'         => 'webinar',
                    'posts_per_page'    => -1,
                    'orderby'           => 'meta_value',
                    'order'             => 'ASC',
                    'meta_query' => array(
                        array(
                            'key' => 'date_and_time',
                            'value' => $today,
                            'compare' => '>='
                      )
                    ),
                ));
               //var_dump( $futureposts_buisness );
                if( $futureposts_all ) : ?>
 
                <div class="row">
 
                <?php foreach( $futureposts_all as $post ) : setup_postdata( $post ); ?>


                    <div class="col-md-4"> 
                        <div id="block_5ed527ab633fb" class="wp-block-classic align block-desktop"> 
                            <div style="height: 600px !important;">      
                                <a class="aug20 card" style="cursor: pointer;"><p></p> 
                                <div class="card-image">
                                    <?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
                                    <img src="<?php echo $url ?>" alt="Featured image of this webinar"/>
                                </div> 
                                <div class="card-text"> 
                                    <h3>
                                    <?php
                                        $seo_friendly_title = get_field( 'seo_friendly_title' );
                
                                            if ( $seo_friendly_title ) {
                                                echo $seo_friendly_title;
                                            } else {
                                                the_title();
                                            }
                                    ?>
                                    </h3>

                                    <p class="date"><?php echo the_field( 'date_and_time' ); ?> CDT</p>
                                    <p><?php the_excerpt(); ?></p> 
                                </div> 
                                </a>
                                <a class="card" style="cursor: pointer;"></a>
                                <p class="center-mob"><a class="plumb-button navy-ghost" href="<?php the_permalink(); ?>">Register</a> </p>
                            
                            </div> 
                        </div> 
                    </div>
                    <?php wp_reset_postdata(); ?>
                    <?php endforeach; ?>

                </div> <!-- End ROW -->
 
                <?php else: ?>
 
                <p>No upcoming webinars are scheduled</p>
        
                <?php endif;  ?>

            </div>
        </div><!-- END All future webinars -->

Open in new window



jQuery
jQuery(document).ready(function($) {
    $("#selectMe").change(function() {
        $(this).find("option:selected").each(function(){
            if($(this).attr("value")=="all-future"){
                $(".tabbed-content").not(".all-future").hide();
                $(".all-future").show();
            }else if($(this).attr("value")=="business-management"){
                $(".tabbed-content").not(".buisness-future").hide();
                $(".buisness-future").show();
            }else if($(this).attr("value")=="financials"){
                $(".tabbed-content").not(".financial-future").hide();
                $(".financial-future").show();
            }else if($(this).attr("value")=="project-management"){
                $(".tabbed-content").not(".project-future").hide();
                $(".project-future").show();
            }else if($(this).attr("value")=="sales-marketing"){
                $(".tabbed-content").not(".sales-future").hide();
                $(".sales-future").show();
            }else if($(this).attr("value")=="virtual-training"){
                $(".tabbed-content").not(".virtual-future").hide();
                $(".virtual-future").show();
            }else{
                $(".tabbed-content").hide();
            }
        });
    }).change();
});

Open in new window

PHPjQueryWordPress

Avatar of undefined
Last Comment
leakim971
Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

Check this snippet code below

$(document).ready(function() {
    
  $("#selectMe
option[value=all-future]").attr('selected', 'selected');
   
});

Open in new window

Avatar of Jon Imms
Jon Imms
Flag of United States of America image

ASKER

still not showing, using that snippet.
Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

noted. you can call out change() function afterward.

Here is the changes

jQuery(document).ready(function($) {

$("#selectMe option[value=all-future]").attr('selected', 'selected');
$("#selectMe").change();
 
});

Open in new window


ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of leakim971
leakim971
Flag of Guadeloupe image

your code already fire the "change" event at page load, so maybe it's a typo issue here : buisness-future
instead : business-future

a shortcut of your code if you can't change classes name to match options values :

<script>
    jQuery(function($) {
        $("#selectMe").change(function() {
            var optionsToClasses = {
                "all-future": ".all-future",
                "business-management": ".buisness-future",
                "financials": ".financial-future",
                "project-management": ".project-future",
                "sales-marketing", ".sales-future",
                "virtual-training": ".virtual-future"
            }
            $(".tabbed-content").hide();
            $("option:selected", this).each(function(){
                $(optionsToClasses[$(this).val()]).show();
            });
        }).change();
    });
</script>

Open in new window



Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Just in case you missed what I was saying in my post here is a sample that uses the code I posted above.

https://www.marcorpsa.com/ee/t3931.html

The code uses the method of setting the selected value on page load (this could be done by adding the 'selected' attribute to the <option>
<option value="all-future" selected>Topic</option>

Open in new window

It shows the all-future content on load (again this could have been done on page render by defaulting the .all-future tabbed content to a visible state.

Finally it demonstrates the shortened version of the change code to work generically (without adding any more classes than you have already) to hide the visible content and show the selected.
Note - you don't have to specifically .not() the selected tab - you can hide everything and then show selected.

Avatar of Jon Imms
Jon Imms
Flag of United States of America image

ASKER

Hi guys,  I thought this was working, but it's starting to not show on page load again, and then refreshing messes things up.

Here is my staging site - staging site

So i have 2 drop downs,  top one will show future webinars,  bottom one will show past webinars.

Here is the jQuery i've been using.  I think i've tried each solution here, and i get the same problems.

/* -- Future Webinars --*/
jQuery(document).ready(function($) {
$("#selectMe option[value=all-future]").attr('selected', 'selected');
        $("#selectMe").change();
         
        });
jQuery(document).ready(function($) {
     $("#selectMe").change(function() {
        $(this).find("option:selected").each(function(){
            if($(this).attr("value")=="all-future"){
                $(".tabbed-content").not(".all-future").hide();
                $(".all-future").show();
            }else if($(this).attr("value")=="business-management"){
                $(".tabbed-content").not(".business-future").hide();
                $(".business-future").show();
            }else if($(this).attr("value")=="financials"){
                $(".tabbed-content").not(".financial-future").hide();
                $(".financial-future").show();
            }else if($(this).attr("value")=="project-management"){
                $(".tabbed-content").not(".project-future").hide();
                $(".project-future").show();
            }else if($(this).attr("value")=="sales-marketing"){
                $(".tabbed-content").not(".sales-future").hide();
                $(".sales-future").show();
            }else if($(this).attr("value")=="virtual-training"){
                $(".tabbed-content").not(".virtual-future").hide();
                $(".virtual-future").show();
            }else{
                $(".tabbed-content").hide();
            }
        });
    }).change();
});


/* -- Past webinars -- */
jQuery(document).ready(function($) {
    $("#selectMePast option[value=all-past]").attr('selected', 'selected');
            $("#selectMePast").change();
             
            });
jQuery(document).ready(function($) {
    $("#selectMePast").change(function() {
        $(this).find("option:selected").each(function(){
            if($(this).attr("value")=="all-past"){
                $(".tabbed-content").not(".all-past").hide();
                $(".all-past").show();
            }else if($(this).attr("value")=="business-management"){
                $(".tabbed-content").not(".business-past").hide();
                $(".business-past").show();
            }else if($(this).attr("value")=="financials"){
                $(".tabbed-content").not(".financial-past").hide();
                $(".financial-past").show();
            }else if($(this).attr("value")=="project-management"){
                $(".tabbed-content").not(".project-past").hide();
                $(".project-past").show();
            }else if($(this).attr("value")=="sales-marketing"){
                $(".tabbed-content").not(".sales-past").hide();
                $(".sales-past").show();
            }else if($(this).attr("value")=="virtual-training"){
                $(".tabbed-content").not(".virtual-past").hide();
                $(".virtual-past").show();
            }else{
                $(".tabbed-content").hide();
            }
        });
    }).change();
});

Open in new window



Avatar of Jon Imms
Jon Imms
Flag of United States of America image

ASKER

Hey Julian,

So i followed what you did on that test site, and i do get it to show on page load, but when i select another option from the dropdown, they all stay as display:none ?

/* -- Future Webinars --*/
jQuery(function($) {
    $(function() {
        const selectMe = document.getElementById('selectMe');
     
        // Select default value (although this could have been done by 
        // adding the selected attribute to the 'all-future' option above)
        selectMe.value = 'all-future';
     
        // Just show it on page load
        $(".all-future").show();
     
       $("#selectMe").change(function() {
          // Get the selected value
          const val = this.value;
     
          // Hide all 
          $('.tabbed-content').hide();
     
          // If a value is selected then show it
          if (val) {
            $('.' + val).show();
          }
       })
    });
});

Open in new window

Avatar of leakim971
leakim971
Flag of Guadeloupe image

here a shortcut for both :
<script>
    jQuery(function($) {
        const handler = function() {
            var optionsToClasses = {
                "all-future": ".all-future",
                "business-management": ".buisness-future",
                "financials": ".financial-future",
                "project-management": ".project-future",
                "sales-marketing", ".sales-future",
                "virtual-training": ".virtual-future"
            }
            $(".tabbed-content").hide();
            $("select[id^='selectMe'] option:selected").each(function() {
                    $(optionsToClasses[$(this).val()]).show();
            })
        }
        $("select[id^='selectMe']").change(handler);
        handler(); // run it at page load
    });
</script>

Open in new window


Avatar of Jon Imms
Jon Imms
Flag of United States of America image

ASKER

leakim971 - thank you.  I'm using your code - staging website
But it still wont show on page load.  Not sure what i am doing wrong?  
Avatar of leakim971
leakim971
Flag of Guadeloupe image

the code is actually never called because you've JavaScript error in the page before

User generated image
Avatar of Jon Imms
Jon Imms
Flag of United States of America image

ASKER

I commented that function out.  
Avatar of leakim971
leakim971
Flag of Guadeloupe image

still have at least 6 JS errors
+2 404 errors

you can set a "debugger" keyword before your code so you know it should stop there if its going to be executed

debugger; 
alert("reached");// or use an alert
jQuery(document).ready(f..

Open in new window