Skip to content

Instantly share code, notes, and snippets.

@manishsongirkar
Created September 10, 2014 11:42
Show Gist options
  • Save manishsongirkar/80b39edc15c304193ba6 to your computer and use it in GitHub Desktop.
Save manishsongirkar/80b39edc15c304193ba6 to your computer and use it in GitHub Desktop.
Show short text and append Read More link, instead of full text by using jQuery. After click on Read More, show full text.
jQuery( document ).ready( function() {
/**
* Show Short Content
* Add Read More link after short text,
* Show full content after click on Read More
*
* Usage:
* jQuery( '.my-selector' ).rtpCharLength();
*/
( function( $ ) {
$.fn.rtpCharLength = function( options ) {
var defaults = {
count: 450,
readmoretext: 'Read More »',
moreclass: 'morelink',
hellip: '…'
};
var settings = $.extend( {}, defaults, options ),
element = this,
full_text = this.html(),
short_text = this.text(),
c = short_text.substr( 0, settings.count );
if ( short_text.length > settings.count ) {
var html = c + settings.hellip + ' <a role="link" href="#" class="' + settings.moreclass + '" title="' + settings.readmoretext + '">' + settings.readmoretext + '</a>';
this.html( '<p class="short-desc">' + html + '</p>' );
}
if ( $( '.' + settings.moreclass ).length > 0 ) {
$( 'body' ).on( 'click touchstart', '.' + settings.moreclass, function( e ) {
element.html( full_text );
e.preventDefault();
} );
}
}; // rtpCharLength();
return this;
}( jQuery ) );
/**
* Show Short text on Designers and Members page
*/
jQuery( '.rtp-about-member .post-content' ).rtpCharLength();
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment