View Full Version : click to read more
jjmaster-j
May 30th, 2009, 11:20 PM
Before I google around, is there a tutorial on how to expand a paragraph when a link is clicked. What I am trying to do is to limit my display on the web page. This is what I have:
Paragraph 1
this is paragraph 1, click here to see more
Paragraph 2
this is paragraph2 after a user click on it.
word word word word word word
Paragraph 3
this is paragraph 3, click here to see more
Thanks
NeoDreamer
May 31st, 2009, 12:51 AM
I wrote a short solution for you. Some people more knowledgeable in JS would know how to create the expand function without the parameter, but I'm no pro.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
/* ================================================== =====
expand
args: string - paragraph's id
ret: void
about: expands paragraph and shows all the text
-----------------------------------------------------------*/
function expand(paragraphName)
{
document.getElementById(paragraphName).className = "";
}
</script>
<style type="text/css">
<!--
/* the paragraph */
p {
width: 30em;
overflow: hidden;
position: relative;
}
/* when the paragraph is not expanded, it will be this tall */
.contracted {
height: 4em;
}
/* the "click to see more" icon */
p img {
position: absolute;
right: 0;
bottom: 0;
}
/* show that it's clickable */
p img:hover {
cursor: pointer;
}
-->
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<p id="paragraph1" class="contracted">
<img src="http://www.iconfinder.net/data/icons/tango/32x32/actions/format-indent-more.png" alt="click to see more" onclick="expand('paragraph1')"/>
"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.
</p>
</body>
</html>
icio
May 31st, 2009, 06:57 AM
Stupid thread title, and stupid reason for a thread. "Before I google around." Are we your *****es? No, we are not.
Nevo
May 31st, 2009, 06:44 PM
Stupid thread title, and stupid reason for a thread. "Before I google around." Are we your *****es? No, we are not.
I was thinking exactly the same, but wouldn't of said it. :angel:
NeoDreamer
May 31st, 2009, 08:10 PM
I answered him because I don't mind practicing coding. I'm interviewing with web companies and they ask similar questions. My solution only works in FF, though.
BTW, does anyone know what I asked for? The image inside the paragraph triggers a function to expand the paragraph. Is there anyway to get the image's parent DOM node without having to pass it into the function?
icio
June 1st, 2009, 01:35 PM
There certainly is, but you still of course have to pass it into the function: just not by ID.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
/* ================================================== =====
expand
args: element - the paragraph
ret: void
about: expands paragraph and shows all the text
-----------------------------------------------------------*/
function expand(paragraph)
{
paragraph.className = "";
}
</script>
<style type="text/css">
<!--
/* the paragraph */
p {
width: 30em;
overflow: hidden;
position: relative;
}
/* when the paragraph is not expanded, it will be this tall */
.contracted {
height: 4em;
}
/* the "click to see more" icon */
p img {
position: absolute;
right: 0;
bottom: 0;
}
/* show that it's clickable */
p img:hover {
cursor: pointer;
}
-->
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<p id="paragraph1" class="contracted">
<img src="http://www.iconfinder.net/data/icons/tango/32x32/actions/format-indent-more.png" alt="click to see more" onclick="expand(this.parentNode)"/>
"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.
</p>
</body>
</html>
NeoDreamer
June 1st, 2009, 01:50 PM
And I figured out why my code doesn't work in IE. It doesn't understand "className". Better use a javascript framework next time so I don't have to do if/else checks on the browser.
icio
June 1st, 2009, 02:27 PM
Your code works just fine in IE. Are you sure it wasn't just some stupid security setting stopping the script from running?
NeoDreamer
June 1st, 2009, 03:56 PM
Yeah, I had to turn off security to make it work. Expanding paragraphs are really dangerous these days. They could potentially take over your whole computer! :)
icio
June 2nd, 2009, 04:20 AM
You never know: Their content might instigate something naughty!
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.