user2571547
user2571547

Reputation: 87

Looking for "show more" "hide" javascript/jquery

I have been searching all day on google for this, basically I have a field that can have anywhere from 50 to 500 characters, so I am looking for a script that allows me to display 200 at a time(as example) with a read more link to expand the rest of the content, so far the closest I have found is this(see code below) but that requires manually separating the content up which is not really possible..

 <p>...This is all visible content... 
<a href="#" id="example-show" class="showLink" 
onclick="showHide('example');return false;">See more.</a>
</p>
 <div id="example" class="more">
    <p>...This content is hidden by default...</p>
<p><a href="#" id="example-hide" class="hideLink" 
onclick="showHide('example');return false;">Hide this content.</a></p>

Where I need something like..

<div class="hidden"><?php $rows['content']; ?></div>

Even if there was a non script PHP way to do this I would be happy.

Upvotes: 0

Views: 1990

Answers (1)

Daniel Ezra
Daniel Ezra

Reputation: 1444

Html

<div class="box">
   <p id="id_full_text" class="class_full_text">
      Full Text
   <p>
   <a href="id_link_show_more" class="class_link_show_more">Show more</a>
   <p id="id_hide_text" class="class_hide_text">
     Hide Text
   <p>
    <a href="id_link_hide" class="class_link_hide">Hide more</a>
</div>

css

.class_hide_text, .class_link_hide {display: none;}

Jquery (in you have just 1 in the same page)

$('#id_link_show_more').click(function () {
    $('#id_full_text').hide(); // hide fullText p
    $('#id_link_show_more').hide(); //hide show button
    $('#id_hide_text').show('100');  // Show HideText p
    $('#id_link_hide').show('100');  // show link hide
 });   
 $('#id_link_hide').click(function () {
        $('#id_link_hide').hide();  // hide link hide
        $('#id_hide_text').hide();  // hide the hide Text 
        $('#id_link_show_more').show('100'); //show ths show button
        $('#id_full_text').show('100'); // show fullText 

     });

Jquery (if you have more than 1 in the same page, because you don't want open all the hide divs in the page)

$('.class_link_show_more').click(function () {
   var the_parent = $(this).parent();
    the_parent.children('.class_full_text').hide();  // hide fullText p
    the_parent.children('.class_link_show_more').hide(); //hide button
    the_parent.children('.class_link_hide').show('100');  // Show HideText p
    the_parent.children('.class_hide_text').show('100');  // Show HideText p

 });
$('.class_link_hide').click(function () {
   var the_parent = $(this).parent();
    the_parent.children('.class_link_hide').hide();  // hide link hide
    the_parent.children('.class_hide_text').hide();  // hide hide text p
    the_parent.children('.class_link_show_more').show('100'); //Show link show
    the_parent.children('.class_full_text').show('100;);  // show full text
 });

Note: the number into the show(x) is the time (millisecond) to show the div

Upvotes: 1

Related Questions