Create a Paragraph Preview with Prototype.js
This is a short preview of the paragraph (More)
so when the user clicks on "More" the full paragraph will be displayed like so:
This is a short preview of the paragraph and here is the more text you wanted to see.(Less)
You can see the simple example in action at http://tech-cats.net/blog/examples/createParagraphPreview.html
Here is how to create this with some simple HTML and JavaScript. The JavaScript relies on Prototype.js but it can be easily re-implemented with a different JavaScript framework.
<div>
This is a short preview of the paragraph
<div id="moreText" style="display: none">
and here is the more text you wanted to see.
</div>
<a class="showMore" href="#" rel="moreText">
<span id="do_more_less">More</span>
</a>
</div>
And the JavaScript to do the magic:
<script type="text/javascript">
// For each link element that has the class name "showMore",
// tie the 'click' event to the 'showHide' function
Event.observe(window, 'load',
function() {
$$('a.showMore').invoke('observe', 'click', showHide);
});
function showHide(event)
{
// Use the event to get the id of the element that invoked the function
var currentElement = Event.element(event);
// Get the parent node of that element and the value of it's "rel" attribute
var toggleContainer = $(currentElement).parentNode.getAttribute('rel');
// Get the toggle caption based on the html of the clicked element
var toggleCaption = ($(currentElement).innerHTML.toLowerCase() == "more") ? "Less" : "More";
// Update the current element with the new caption value
$(currentElement).update(toggleCaption);
// If the container to be toggled exists, toggle it's visibility
if ($(toggleContainer)) {
$(toggleContainer).toggle();
}
// Return false so the link does not go anywhere
return false;
}
</script>
Something to keep in mind is that you cannot simply copy the JavaScript in the head of the HTML page. That is because the line "$$('a.showMore').invoke('observe', 'click', showHide);", needs to fire after the HTML has been rendered. To resolve this, we can one of two things:
- Put the script in the head tag but wrap it inside a window load observer or dom:loaded event (if you are using Prototype v1.6) like so:
Event.observe(window, 'load',
or
function() {
$$('a.showMore').invoke('observe', 'click', showHide);
});document.observe("dom:loaded",
function() {
$$('a.showMore').invoke('observe', 'click', showHide);
}); - Put the script as the last thing on the page, after the html.
The code can also be downloaded at http://tech-cats.net/blog/examples/createParagraphPreview.txt
I was born in Bulgaria. My immediate family and I relocated to Syracuse, NY in 1995. I completed high school in Syracuse, and then continued my education at Alfred University. My major at Alfred University was Computer Science. I also obtained a minor in Management Information Systems (MIS) to bridge the gap between technology and the business world. One of my future goals is to extend that bridge by obtaining a Master's degree in Business Administration. Boyan is a DZone MVB and is not an employee of DZone and has posted 26 posts at DZone. You can read more from them at their website.
- Login or register to post comments
- 2500 reads
- Printer-friendly version
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)









