Go Back   CodingForums.com > :: Client side development > HTML & CSS

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-03-2013, 10:00 AM   PM User | #1
garevn
New Coder

 
Join Date: Apr 2011
Posts: 95
Thanks: 13
Thanked 1 Time in 1 Post
garevn is an unknown quantity at this point
expand text "read more"

I want somehow to expand and collapse a text to a spesific area.
For example lets say i have a text of 5 rows and i want to show only 2 like:

Code:
1row
2row...read more

if u press "read more" then:

1row
2row
3row
4row
5row...read less
Is there a way to do that?
garevn is offline   Reply With Quote
Old 02-03-2013, 05:58 PM   PM User | #2
Excavator
Master Coder


 
Excavator's Avatar
 
Join Date: Dec 2006
Location: Alaska
Posts: 9,410
Thanks: 22
Thanked 1,765 Times in 1,749 Posts
Excavator has a spectacular aura aboutExcavator has a spectacular aura aboutExcavator has a spectacular aura about
Hello garevn,
There's a cool little jQuery script that does that nicely here.

You can google for lots more if that one doesn't suit you.
__________________
Validate often DURING development - Use it like a splelchecker | Debug during Development |Write it for FireFox, ignore IE
Use the right DocType | Validate your markup | Validate your CSS | Why validating is good | Why tables are bad
Excavator is offline   Reply With Quote
Old 02-03-2013, 09:16 PM   PM User | #3
Frankie
Regular Coder

 
Join Date: Sep 2011
Posts: 307
Thanks: 3
Thanked 35 Times in 35 Posts
Frankie is an unknown quantity at this point
Quote:
Originally Posted by Excavator View Post
Hello garevn,
There's a cool little jQuery script that does that nicely here.
One doesn't need jQuery plus a plugin script for that -- there is a much easier way. Have a look at this: http://jsfiddle.net/NXpHe/. And here is the full code in one file:

Code:
<!DOCTYPE HTML>
<html>
<head>
     <title>Toggle Display Demo</title>
<script>
function toggleDisplay(id,a)
{
      var elaboration = document.getElementById(id);
      if (elaboration.style.display == "block")
      {
        elaboration.style.display = "none";
        if(a) a.innerHTML="Read more..."; // won't work with target=_self
      }
      else
      {
        elaboration.style.display = "block";
        if(a) a.innerHTML = "Fold in...";
      }
}
</script>
<style type="text/css">
    .toggleLink {
        color: red;
        text-decoration: underline;
    }
    .toggleLink:hover {
        cursor: pointer;
    }
    .elaboration {
        display: none;
    }
</style>
</head>
<body>
    <p>Lorem ipsum ... <span class="toggleLink" onclick="toggleDisplay('elaboration_1',this)">Read more...</span> <span class="elaboration" id="elaboration_1">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</span></p>
</body>
</html>
__________________
Frank

How to: Target IE in, Position in, Center in, Create a Fixed ('Sticky') Footer with, and Create a Drop-Down/Fly-Out Menu with CSS: Website Laten Maken Amsterdam.

Last edited by Frankie; 02-03-2013 at 09:43 PM..
Frankie is offline   Reply With Quote
Old 02-04-2013, 07:06 PM   PM User | #4
hdewantara
Regular Coder

 
hdewantara's Avatar
 
Join Date: Aug 2009
Location: Jakarta, Indonesia.
Posts: 289
Thanks: 5
Thanked 40 Times in 40 Posts
hdewantara is an unknown quantity at this point
If you don't mind a bit of complicated structure,
and base the expanding/ collapsing not on a "press",
but on "focus" event, then let's see the following:
Code:
<html>
<head>
<style type="text/css">
	div + div > div{
		display: none;
	}
	div > a.readMore:focus{
		display: none;
	}
	div > a.readMore:focus + div{
		display: block;
	}
</style>
</head>
<body>
	<div>row #1...</div>
	<div>
		<span>row #2...</span>
		<a class="readMore" href="#">read more</a>
		<div>
			<div>row #3...</div>
			<div>row #4...</div>
			<div>
				<span>row #5...</span>
				<a href="#">read less</a>
			</div>
		</div>
	</div>
</body>
</html>
hdewantara is offline   Reply With Quote
Old 02-04-2013, 10:59 PM   PM User | #5
alliv105
New Coder

 
Join Date: Jan 2013
Posts: 14
Thanks: 0
Thanked 1 Time in 1 Post
alliv105 is an unknown quantity at this point
You don't really need to do much - it's very simple. Create a hidden div inside your HTML somewhere with the "extra" text, and also create a hidden form field with value="false" (or something of that nature, ie. yes/no). Keep the form field outside of the div. When the link is clicked, send it to a javascript function which will toggle the style settings of that originally hidden div element. In the function, read the value of the hidden text field. If it's "false", set the value to "true" and set the style settings on the div to "display:block". Vice versa.
__________________
Owner of online bath hardware and accessories store, selling bathroom accessory sets | Blog.
alliv105 is offline   Reply With Quote
Old 02-05-2013, 02:23 AM   PM User | #6
Frankie
Regular Coder

 
Join Date: Sep 2011
Posts: 307
Thanks: 3
Thanked 35 Times in 35 Posts
Frankie is an unknown quantity at this point
Quote:
Originally Posted by alliv105 View Post
You don't really need to do much - (....) Vice versa.
What exactly is it that makes you think that the OP would understand one word of what you wrote? S/he doesn't even know whether what s/he wants is possible...
__________________
Frank

How to: Target IE in, Position in, Center in, Create a Fixed ('Sticky') Footer with, and Create a Drop-Down/Fly-Out Menu with CSS: Website Laten Maken Amsterdam.
Frankie is offline   Reply With Quote
Old 02-05-2013, 02:52 AM   PM User | #7
alliv105
New Coder

 
Join Date: Jan 2013
Posts: 14
Thanks: 0
Thanked 1 Time in 1 Post
alliv105 is an unknown quantity at this point
My fault. My intention wasn't to give them the answer but to help them solve it themselves. Knowledge is power, and learning about divs, classes, javascript functions, and form fields is something well worth learning.
__________________
Owner of online bath hardware and accessories store, selling bathroom accessory sets | Blog.
alliv105 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:40 AM.


Advertisement
Log in to turn off these ads.