Web Development Blog

Twitter

14 Sep, 2009

Read more using jQuery live event

Web Development » CSS, Snippets, xHTML » Read more using jQuery live event

The main purpose of this code is to have “Read more” for a long block of text (eg: comments) without having to separate the text into two div blocks and then use toggle to show/hide the 2nd block. This code simply uses CSS overflow:hidden property to hide long block of text. jQuery live event is used to ensure it works for content loaded using AJAX.

CSS

#comments .expandable .comment-body {
	height: 50px;
        overflow: hidden;
}

Javascript

$("#comments .comment-more a").live("click", function(){
	if ($(this).text() == 'more') {
		$(this).parent().parent().removeClass("expandable");
		$(this).text("less");
	} else {
		$(this).parent().parent().addClass("expandable");
		$(this).text("more");
	}
	return false;
});

HTML

<div id="comments">
	<div class="comment-content expandable">
		<div class="comment-body">
		<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed mattis cursus adipiscing. Donec risus leo, venenatis adipiscing euismod eget, ultricies vitae nunc. Curabitur fermentum tortor vitae nisl fermentum non scelerisque erat posuere. Sed auctor tempor venenatis. Morbi vel urna turpis, adipiscing molestie nisl. Curabitur ut lorem id neque mollis fringilla. Phasellus varius viverra suscipit. Proin eleifend quam in diam dictum suscipit. Praesent eleifend fringilla turpis sit amet commodo. In ullamcorper ligula non augue bibendum ultricies scelerisque tortor congue. Aenean nulla lectus, tincidunt egestas cursus sit amet, tristique non libero. Duis facilisis blandit nisi, eget cursus turpis tristique non. Fusce sodales dolor vel ligula rutrum venenatis. Cras molestie vulputate urna at iaculis. Mauris consequat accumsan mi, non facilisis enim porttitor gravida.</p>
		<p>Suspendisse et purus a tortor aliquet vestibulum. Sed ullamcorper euismod interdum. Aenean gravida, metus ac imperdiet venenatis, neque ligula viverra urna, a pellentesque elit mi id ligula. In suscipit nunc eget magna bibendum tempor. Nunc ac mauris ut risus posuere accumsan. Nulla dictum semper justo, quis adipiscing sem consectetur quis. Donec felis arcu, convallis pretium consectetur at, semper ullamcorper urna. Proin elit nisi, vehicula eget pretium ac, faucibus vitae justo. Praesent quis arcu eget massa interdum ullamcorper in nec mauris. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Praesent orci nisl, mollis et egestas ut, suscipit et orci. Morbi in justo sit amet ligula facilisis interdum ac id ante. Nam sit amet metus eget augue tristique interdum vitae in metus. Vestibulum tristique vestibulum lorem, eget pharetra ante aliquet eget. Donec fermentum adipiscing sagittis. Quisque interdum, augue non vulputate rutrum, sem tortor facilisis diam, in tincidunt nisi tellus ut eros. </p>
		</div>
		<span class="comment-more"><a href="#">more</a></span>
	</div>

	<div class="comment-content expandable">
		<div class="comment-body">
		<p>Vivamus viverra arcu non nibh sollicitudin consectetur. Vivamus sodales erat a tellus semper in feugiat eros pharetra. Aenean vel magna magna, scelerisque molestie risus. Fusce lobortis euismod justo, sed gravida neque porta nec. Aenean risus metus, accumsan et mattis et, semper non erat. Fusce velit enim, viverra a pulvinar nec, viverra vel erat. Praesent sed mi quam, vel dignissim nunc. Nullam accumsan quam ut eros congue eget accumsan arcu bibendum. Nam congue egestas fringilla. Aliquam felis mi, volutpat nec tempor eu, hendrerit quis lorem. Pellentesque pretium metus quis sapien luctus venenatis vitae pretium dolor. Fusce ac tellus a tellus posuere suscipit a quis ante. Quisque ut sem eget risus consequat ornare.</p>
		<p>Nunc id risus risus, sed pellentesque nisi. Donec pulvinar nisl sit amet eros venenatis ut dictum odio pretium. Nulla hendrerit ligula a metus rutrum sed feugiat libero mattis. Nam id mi ac leo iaculis volutpat. Maecenas nec enim ut mauris convallis auctor in sed purus. Nam porta magna vitae nisl tempus in eleifend felis viverra.</p>
		</div>
		<span class="comment-more"><a href="#">more</a></span>
	</div>
</div>

Recommended Books on jQuery

Other similiar posts that you might be interested in:

3 Responses to "Read more using jQuery live event"

1 | Handling IE6 | WebDesignExpert.Me

September 16th, 2009 at 12:36 am

Avatar

[...] Implementing “read more” using jQuery live event – Link. [...]

2 | ??????? » [Web] ????

September 22nd, 2009 at 2:07 pm

Avatar

[...] Read more using jQuery live event [...]

3 | Octavio Corral

September 29th, 2009 at 2:09 pm

Avatar

very nice. simple yet effective.

Comment Form

ebook
Subscribe to our newsletter and receive FREE e-book "7 Days Exercise to Build More Traffic To Your Blog"
Your name:  
Your email:  

  • About
    The blog Web Development Blog is where I keep myself up to date with the latest technologies in the industry and share my ideas and thoug...
  • Adsense Tips and Tricks
    With personal and commercial blogs, community websites, and social pages becoming the hot topic of the Internet in the last few years, many...
  • Advertise With Us
    Web Development Blog is a blog about Web Development, Web Design, Web Applications, Web 2.0, AJAX, Search Engine Optimisation, Latest Techno...
  • Archive
    Grab yourself a cup of coffee or tea, sit back and browse through an archive of all the blog posts on Web Development Blog....
  • Choosing a Content Management System
    The most commonly asked question when it comes to Content Management System is whether to build or buy (pre-built). While there are many fa...
  • Coupons
    Use the following coupon codes, promos and discount codes to save money on web hosting, domain names, website templates, ebooks, software, p...
  • Customised WordPress Themes
    If you would like a customised WordPress theme for your blog or your website, read on: (or head to WordPress Themes page to get free WordPre...
  • Favourite Poems
    If by Rudyard Kipling If you can keep your head when all about you Are losing theirs and blaming it on you, If you can trust yourself w...
  • Favourites
    Books Web Development Books Magazines Glamour Reader's Digest Software Adobe Photoshop CS4 Trend Micro Antivirus + Antispyw...
  • How to Start a Blog?
    What's a blog? According to Wikipedia, a blog is a user-generated website where entries are made in journal style and displayed in a reve...
  • Introduction to JSP Standard Tag Library (JSTL Basics)
    Tag libraries to include in your .jsp page <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> &l...
  • Links
    Free Anonymous Proxy Increases your privacy and security on the Internet by using our free, fast and easy to use web proxy. data backu...
  • Making a Passive Income Online
    Passive income, in my opinion, means an income which does not require a lot of regular maintenance work or continuing effort. I have been t...
  • Online Distribution Channels
    One of the importances of any website or business is to bring your products or services to the right people and to reach the target audience...
  • Recent Projects
    Followings are some of the recent projects I have done in 2006 -- 2007. Check out my latest business venture Web design Sydney to get your...
  • Recommended Web Development Books
    Many developers often ask me what books I read and what books I recommend in regards to web development and web technologies such as AJAX, J...
  • Search
    Looking for something on Web Developement Blog? Use this search tool powered by Google Custom Search to find what you are looking for just ...
  • Seven Tips to Building an Online Presence for your Business
    1. Get a domain name for your business The first and foremost step in building an online presence is to secure a domain name for your busi...
  • Seven Tips to Increasing Your Website’s Traffic Using SEO
    SEO is the buzz word - many people have mentioned it and many have heard of it too, but very few people know how to implement it properly. ...
  • Subscribe
    Newsletter [newsletter] RSS 2.0 feeds Entries feed Comments feed Subscribe using your favourite web-based or desktop feed r...
  • Useful Linux Commands
    Find files older than 60 days find * -mtime +60 Delete files in backup folder which are older than 60 days rm -f `find /backup/ -mtime ...
  • Web Design Tips
    Nowadays, having a website for your business is like having a phone number. Almost every business has their own website, with their own doma...
  • Web Development Books
    Many developers often ask me what books I read and what books I recommend in regards to web development and web technologies such as AJAX, J...
  • Web Hosting Comparison Chart
    The following are some of the most popular web hosting providers. I have personally used the recommended ones. Hosting coupon codes will ...
  • Web Standards
    Web Standards is defined as a set of rules or specifications that should be followed when developing a website.  The main objectives o...
  • WordPress Themes
    All WordPress themes are designed and coded by esn studio, and licensed under GPL license. If you would like a customised WordPress theme fo...
  • Sam: Useful. I always forget the rel target one
  • Chronic Tinnitus: Hi, Thanks for taking the time to discuss a really confusing matter - I find Plesk so much more awkward than the cpanel that I'm used to. Thanks Chr
  • Serhiy: I personally liked the web hosting comparison chart. Especially the recommended *. I got Bluehost :) .-= Serhiy´s last blog ..

Interests

Web development, Web design, Open source technologies, Portal development, APIs, Web services, Social media applications, Search engine optimisation, Mobile application development, iPhone Apps, Web 2.0, Web 3.0, Latest Internet technologies

Misc.

  • bluehost Hosting $6.95/month
  • Joomla Templates