Results 1 to 9 of 9

Thread: Toggle script to show rest

  1. #1
    Join Date
    Jul 2011
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Toggle script to show rest

    Hi,

    I'm looking for a toggle script that shows and hide a large paragraph of text. I have this one here

    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
    $("#hide").click(function(){
    $("p").hide();
    });
    $("#show").click(function(){
    $("p").show();
    });
    });
    </script>
    </head>
    <body>
    <p>If you click on the "Hide" button, I will disappear.</p>
    <button id="hide">Hide</button>
    <button id="show">Show</button>
    </body>


    Which does the hide and show but what i want is to modify it so that it shows the first let's say 12 characters (up to "If you click") then hides the rest. Then once I click on SHOW it displays the rest of the sentence without redisplaying "If you click" again.

    Any help is appreciated.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Confusing. If I understand what you are asking for though:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
     <script type="text/javascript">
     $(document).ready(function(){
     $("#hide").click(function(){
     $("p").hide().find('span').css({display: 'none'}).eq(1).css({display: 'inline'});
     });
     $("#show").click(function(){
     $("p").show();
     });
     });
     </script>
    </head>
     <body>
     <p><span>If you click</span> <span style="display: none;">on the "Hide" button, I will disappear.</span></p>
     <button id="hide">Hide</button>
     <button id="show">Show</button>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jul 2011
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Thanks for the reply...sorry to make it so hard to understand. I have a code that displays the full bio which usually contains over 1000 characters. I'm using a substr to limit the characters displayed. What i wanna do is maybe have a php or javascript so that once they click on the "Read Full Bio" the script toggle the code <? echo nl2br($profile['great_vacation']); ?> without the substr() letting it display the full bio.
    Can you tell me how i would do that please. Would it be better to code it in java so that page doesn't have to reload?? Thanks in advance.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I don't know how to code in java. That's a completely different language than javascript.

    Perhaps this:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
     <script type="text/javascript">
     $(document).ready(function(){
     $(".hide").click(function(){
     $(this).parent("p").find('.story, .hide').hide().end().find('.more').show();
     });
     $(".more").click(function(e){
     e.preventDefault();
     $(this).hide().parent('p').find('.story, .hide').show();
     });
     });
     $('head').append('<style type="text/css">.hide, .story {display: none;}</style>');
     </script>
     <noscript>
     <style type="text/css">
      .more, .hide {
      	display: none;
      }
     </style>
     </noscript>
    </head>
     <body>
     <p>Some Tantalizing Headline <a href="#" class="more">read more . . .</a> <span class="story">happening in London today as those a cappella pessimists benignly agree upon our random parabolas. That appraising performer authoritatively posts your lively planetesimals. Those lugubrious pekingese ably pat those abundant mice. That big planet austerely pokes the rambling goblins. His minuscule megaphones accidentally pray to your abiding fructose. Our miniature moles accessibly add to their befuddling halters. Their absorbing monsters augustly allow much about the mistrusting cats. Those rainy antidisestablishmentarians auspiciously heat those accusing mnemonics. Your approving pitchers believably help the accommodating pheasants. Our ranting boys automatically acknowledge those arrogant partridges.</span><button class="hide">Hide Details</button></p>
     <p>Another Tantalizing Headline <a href="#" class="more">read more . . .</a> <span class="story">happening in Brussels today as that rasping artist benevolently apologizes for your appeasing plantations. An adjacent palindrome already pastes your aching monkeys. Your beautiful philosophers abusively amuse the aspiring men. An admonishing palette aberrantly advises their befitting mosquitoes. The raspy poinsettias ambivalently accept that mimicking mountain. The astonishing partisans abominably heap up his misleading paradigms. His rapid plants autocratically plug the better players. His becalming mugs angularly hook those ashamed banshees. A lingering parrot angrily hovers over her astounding pigs. A rattling mule abhorrently peels those admiring guffaws.</span><button class="hide">Hide Details</button></p>
     
    
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Jul 2011
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Hi John

    Once again thanks for the code...That's actually a perfect toggle script if u ask me...the only problem with that code would be you would have to manually type in what is shown and what is hidden. I'm trying to use this function on my script but I don't it will work because my BIO desciption is stored in this code <? echo nl2br($profile['bio_details']); ?> Therefore I can't insert the <a href="#" class="more"> in the middle of the paragraph. I mean I can place <a href="#" class="more"> at the beginning of <? echo nl2br($profile['bio_details']); ?> but that will make it all hidden until clicked. I want have the BIO details to be shown and half to be hidden till clicked. You know where I'm trying to get at???? sorry if its too confusing

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    PHP and javascript can coexist on the same page and even compliment each other. "PHP you're looking very well today", "Thank you javascript, you don't look so bad yourself."

    If your:

    PHP Code:
    <? echo nl2br($profile['bio_details']); ?>
    doesn't include any HTML, that would be good. But it can also be done by stripping out tags (PHP can do that, selectively if need be). You just have to decide exactly where how to split it. Once you determine that you can do something like:

    PHP Code:
    <p><? echo nl2br($profile['bio_details']['part_one']); ?> <a href="#" 
    class="more">read more . . .</a> <span 
    class="story"><? echo nl2br($profile['bio_details']['part_two']); ?></span><button 
    class="hide">Hide Details</button></p>
    There are other ways, like you could have a $profile['bio_title'] and a $profile['bio_details'] in your database or whatever.

    Or, the splitting could be done via javascript. Here's one way to do that:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
    <script type="text/javascript">
    jQuery('head').append('<style type="text/css">.item, .hide, .story {display: none;}</style>');
    jQuery(function($){
    	$('.item').each(function(){
    		var $t = $(this), html = $t.html(), splitindex = html.indexOf('happening');
    		$t.html(html.substring(0, splitindex) + ' <a href="#" class="more">read more . . .</a> <span class="story">' +
    		html.substring(splitindex) + ' </span><button class="hide">Hide Details</button>').css({display: 'block'});
    	});
    	$('.hide').click(function(){
    		$(this).parent('p').find('.story, .hide').hide().end().find('.more').show();
    	});
    	$('.more').click(function(e){
    		e.preventDefault();
    		$(this).hide().parent('p').find('.story, .hide').show();
    	});
    });
    </script>
    </head>
     <body>
     <p class="item">Some Tantalizing Headline happening in London today as those a cappella pessimists benignly agree upon our random parabolas. That appraising performer authoritatively posts your lively planetesimals. Those lugubrious pekingese ably pat those abundant mice. That big planet austerely pokes the rambling goblins. His minuscule megaphones accidentally pray to your abiding fructose. Our miniature moles accessibly add to their befuddling halters. Their absorbing monsters augustly allow much about the mistrusting cats. Those rainy antidisestablishmentarians auspiciously heat those accusing mnemonics. Your approving pitchers believably help the accommodating pheasants. Our ranting boys automatically acknowledge those arrogant partridges.</p>
     <p class="item">Another Tantalizing Headline happening in Brussels today as that rasping artist benevolently apologizes for your appeasing plantations. An adjacent palindrome already pastes your aching monkeys. Your beautiful philosophers abusively amuse the aspiring men. An admonishing palette aberrantly advises their befitting mosquitoes. The raspy poinsettias ambivalently accept that mimicking mountain. The astonishing partisans abominably heap up his misleading paradigms. His rapid plants autocratically plug the better players. His becalming mugs angularly hook those ashamed banshees. A lingering parrot angrily hovers over her astounding pigs. A rattling mule abhorrently peels those admiring guffaws.</p>
    </body>
    </html>
    Last edited by jscheuer1; 09-17-2011 at 12:50 AM. Reason: add javascript method of splitting
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Jul 2011
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Hi John,

    I just tried out your code and was wondering how did you tell the script to show the first 3 words in the <p> ?? I can't seem to figure it out. Can you script specify to show X amount characters before the cut off?? I'm using this with my code and its not showing any cut off.

    <p class="item"> <? echo ($profile['great_vacation'])?> </p>

    I don't quite understand your code = (.

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Funny you should ask that. I was just playing with the code because I thought there should be options. A number of things have changed in order to get things to line up right using an arbitrary index that could split words. I also added a showspeed option to enhance the effect.

    Anyways, here's the new script configured how I think you may want it:

    Code:
    <script type="text/javascript">
    
    // Read More Script (c)2011 John Davenport Scheuer
    // as first seen in http://www.dynamicdrive.com/forums/
    // username: jscheuer1 - This Notice Must Remain for Legal Use
    
    jQuery('head').append('<style type="text/css">.item, .hide, .story {display: none;}</style>');
    jQuery(function($){
    	var defaultword = false, // set to default 'word' (quoted) to split on, or false (unquoted) to use default index
    	defaultindex = 50, // set to default index (unquoted positive integer) to split on if default word is set to false or is not found in the content
    	showspeed = 'slow'; // 'fast', 'normal', or 'slow', or unquoted integer representing milliseconds
    
    ////////////////////// No Need to Edit Beyond Here //////////////////////
    
    	/*@cc_on @*/
    	/*@if(@_jscript_version >= 5)
    		if($.browser.version < 8){
    			showspeed = '';
    		}
    	@end @*/
    
    	$('.item').each(function(){
    		var $t = $(this), html = $t.html(), word, index,
    		splitindex = $t.attr('data-splitindex') || ((word = $t.attr('data-splitword')) && (index = html.indexOf(word)) > -1? index :
    		defaultword && (index = html.indexOf(defaultword)) > -1? index : defaultindex);
    		$t.empty().append(html.substring(0, splitindex) + '<span class="more"> </span><a href="#" class="more">read more . . .</a><span class="story">' +
    		html.substring(splitindex) + ' </span><input type="button" class="hide" value="Hide Details">').css({display: 'block'});
    	});
    	$('.hide').click(function(e){
    		e.preventDefault();
    		$(this).parent('.item').find('.story, .hide').hide().end().find('.more').show();
    	});
    	$('.more').click(function(e){
    		e.preventDefault();
    		$(this).parent('.item').find('.more').hide().end().find('.story, .hide').show(showspeed);
    	});
    });
    </script>
    Notes:

    Because a p element will self close if it encounters a block level element within itself, it's safer to use div for the elements with class="item":

    Code:
    <div class="item"> <? echo ($profile['great_vacation'])?> </div>
    Ideally though, there should be no html tags in <? echo ($profile['great_vacation'])?>. If there are and you use an arbitrary index, they can get truncated. Even with a splitword or defaultword, the control tags for the script could end up inside other elements, which could cause unexpected display issues or break the script.

    If you want no animation as the content is revealed, set showspeed = ''

    If using a word (defaultword or the data-splitword) and it's not in the content, the script will use the defaultindex.

    There are optional data-splitword or data-splitindex attributes for the class="item" tags. These don't have to be used. If they are used, they override the defaults in the script. This demo shows how to use them:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
    <script type="text/javascript">
    
    // Read More Script (c)2011 John Davenport Scheuer
    // as first seen in http://www.dynamicdrive.com/forums/
    // username: jscheuer1 - This Notice Must Remain for Legal Use
    
    jQuery('head').append('<style type="text/css">.item, .hide, .story {display: none;}</style>');
    jQuery(function($){
    	var defaultword = 'happening', // set to default 'word' (quoted) to split on, or false (unquoted) to use default index
    	defaultindex = 20, // set to default index (unquoted positive integer) to split on if default word is set to false or is not found in the content
    	showspeed = 'slow'; // 'fast', 'normal', or 'slow', or unquoted integer representing milliseconds
    
    ////////////////////// No Need to Edit Beyond Here //////////////////////
    
    	/*@cc_on @*/
    	/*@if(@_jscript_version >= 5)
    		if($.browser.version < 8){
    			showspeed = '';
    		}
    	@end @*/
    
    	$('.item').each(function(){
    		var $t = $(this), html = $t.html(), word, index,
    		splitindex = $t.attr('data-splitindex') || ((word = $t.attr('data-splitword')) && (index = html.indexOf(word)) > -1? index :
    		defaultword && (index = html.indexOf(defaultword)) > -1? index : defaultindex);
    		$t.empty().append(html.substring(0, splitindex) + '<span class="more"> </span><a href="#" class="more">read more . . .</a><span class="story">' +
    		html.substring(splitindex) + ' </span><input type="button" class="hide" value="Hide Details">').css({display: 'block'});
    	});
    	$('.hide').click(function(e){
    		e.preventDefault();
    		$(this).parent('.item').find('.story, .hide').hide().end().find('.more').show();
    	});
    	$('.more').click(function(e){
    		e.preventDefault();
    		$(this).parent('.item').find('.more').hide().end().find('.story, .hide').show(showspeed);
    	});
    });
    </script>
    <style type="text/css">
    .item {
    	margin: 4px;
    }
    </style>
    </head>
     <body>
     <div class="item" data-splitword="London">Some Tantalizing Headline happening in London today as those a cappella pessimists benignly agree upon our random parabolas. That appraising performer authoritatively posts your lively planetesimals. Those lugubrious pekingese ably pat those abundant mice. That big planet austerely pokes the rambling goblins. His minuscule megaphones accidentally pray to your abiding fructose. Our miniature moles accessibly add to their befuddling halters. Their absorbing monsters augustly allow much about the mistrusting cats. Those rainy antidisestablishmentarians auspiciously heat those accusing mnemonics. Your approving pitchers believably help the accommodating pheasants. Our ranting boys automatically acknowledge those arrogant partridges.</div>
     <div class="item" data-splitindex="55">Another Tantalizing Headline happening in Brussels today as that rasping artist benevolently apologizes for your appeasing plantations. An adjacent palindrome already pastes your aching monkeys. Your beautiful philosophers abusively amuse the aspiring men. An admonishing palette aberrantly advises their befitting mosquitoes. The raspy poinsettias ambivalently accept that mimicking mountain. The astonishing partisans abominably heap up his misleading paradigms. His rapid plants autocratically plug the better players. His becalming mugs angularly hook those ashamed banshees. A lingering parrot angrily hovers over her astounding pigs. A rattling mule abhorrently peels those admiring guffaws.</div>
    </body>
    </html>
    Last edited by jscheuer1; 09-17-2011 at 09:12 PM. Reason: English Usage
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. The Following User Says Thank You to jscheuer1 For This Useful Post:

    mikster (09-17-2011)

  10. #9
    Join Date
    Jul 2011
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Hi John,

    What can I say....YOU ARE THE GREATEST PERSON ALIVE Thanks for the code...that's exactly what I needed.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •