Skip to content
Advertisement

Read More functionality code is not working as expected

I have a problem with the “read more” functionality I have implemented into my website. The code is working fine but only after two times of pressing the read more button created with the code. This code works when it detects the WordPress read more tag on the text field in the backend and splits the text into two parts.

enter image description here

I’ll paste a video of the problem here: https://vimeo.com/732701370

You can see also the problem in a page of my website like this one: https://mallorca-select.com/properties-for-sale/villa-project-in-ibiza/

I think that the code is working fine but there is only something that at first it makes the read more to not fold. If anyone can point me in the right direction would be awesome. Thank you

PHP CODE:

<?php 
    if(get_field('property_description')){
        $highlights = get_field('property_description');
        if(strpos($highlights, '<!--more-->') !== false) {
            $start = strstr($highlights, '<!--more-->', true);
            $end = strstr($highlights, '<!--more-->');
            echo "<div class='exsec'>".$start."</div>";
            echo "<div class='fullsec'>".$end."</div><div class='btnRM'><a href=''>READ MORE</a></div>";
        } else {
            echo $highlights;
        }
}?>

JAVASCRIPT CODE:

jQuery('.btnRM a').click(function(e){
    e.preventDefault();
    if(jQuery(this).text()=="READ MORE"){
        jQuery(this).parent().prev('.fullsec').fadeIn();    
        jQuery(this).text('LESS');
    } else {
        jQuery(this).parent().prev('.fullsec').fadeOut();
        jQuery(this).text('READ MORE'); 
    }
    
});

Advertisement

Answer

Based on your video, I see that the hidden text was already shown. So first click at READ MORE button will do nothing and no fade in because the content was already shown (except text changed to LESS). jQuery fadeIn() does not do anything if the element was already shown and the same for fadeOut() for hidden element.

If you want the content hidden at first, update the div element with this <div class='fullsec' style='display: none;'> inside your PHP code. The Javascript code is fine.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement