<div class="comment more">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Vestibulum laoreet, nunc eget laoreet sagittis,
quam ligula sodales orci, congue imperdiet eros tortor ac lectus.
Duis eget nisl orci. Aliquam mattis purus non mauris
blandit id luctus felis convallis.
Integer varius egestas vestibulum.
Nullam a dolor arcu, ac tempor elit. Donec.
</div>
<div class="comment more">
Duis nisl nibh, egestas at fermentum at, viverra et purus.
Maecenas lobortis odio id sapien facilisis elementum.
Curabitur et magna justo, et gravida augue.
Sed tristique pellentesque arcu quis tempor.
</div>
<div class="comment more">
consectetur adipiscing elit. Proin blandit nunc sed sem dictum id feugiat quam blandit.
Donec nec sem sed arcu interdum commodo ac ac diam. Donec consequat semper rutrum.
Vestibulum et mauris elit. Vestibulum mauris lacus, ultricies.
</div>
Code language: HTML, XML (xml)
a {
color: #0254EB
}
a:visited {
color: #0254EB
}
a.morelink {
text-decoration:none;
outline: none;
}
.morecontent span {
display: none;
}
.comment {
width: 400px;
background-color: #f0f0f0;
margin: 10px;
}
Code language: CSS (css)
$(document).ready(function() {
var showChar = 100;
var ellipsestext = "...";
var moretext = "more";
var lesstext = "less";
$('.more').each(function() {
var content = $(this).html();
if(content.length > showChar) {
var c = content.substr(0, showChar);
var h = content.substr(showChar-1, content.length - showChar);
var html = c + '<span class="moreellipses">' + ellipsestext+ ' </span><span class="morecontent"><span>' + h + '</span> <a href="" class="morelink">' + moretext + '</a></span>';
$(this).html(html);
}
});
$(".morelink").click(function(){
if($(this).hasClass("less")) {
$(this).removeClass("less");
$(this).html(moretext);
} else {
$(this).addClass("less");
$(this).html(lesstext);
}
$(this).parent().prev().toggle();
$(this).prev().toggle();
return false;
});
});
Code language: JavaScript (javascript)
/*
* jQuery Shorten plugin 1.0.0
*
* Copyright (c) 2013 Viral Patel
* //www.viralpatel.net
*
* Dual licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*/
(function($) {
$.fn.shorten = function (settings) {
var config = {
showChars: 100,
ellipsesText: "...",
moreText: "more",
lessText: "less"
};
if (settings) {
$.extend(config, settings);
}
$(document).off("click", '.morelink');
$(document).on({click: function () {
var $this = $(this);
if ($this.hasClass('less')) {
$this.removeClass('less');
$this.html(config.moreText);
} else {
$this.addClass('less');
$this.html(config.lessText);
}
$this.parent().prev().toggle();
$this.prev().toggle();
return false;
}
}, '.morelink');
return this.each(function () {
var $this = $(this);
if($this.hasClass("shortened")) return;
$this.addClass("shortened");
var content = $this.html();
if (content.length > config.showChars) {
var c = content.substr(0, config.showChars);
var h = content.substr(config.showChars, content.length - config.showChars);
var html = c + '<span class="moreellipses">' + config.ellipsesText + ' </span><span class="morecontent"><span>' + h + '</span> <a href="#" class="morelink">' + config.moreText + '</a></span>';
$this.html(html);
$(".morecontent span").hide();
}
});
};
})(jQuery);
Code language: JavaScript (javascript)
<script type="text/javascript"
src="https://www.viralpatel.net/demo/jquery/jquery.shorten.1.0.js"></script>
Code language: HTML, XML (xml)
Step 2: Add the code to shorten any DIV content. In below example we are shortening DIV with class “comment”. <div class="comment">
This is a long comment text.
This is a long comment text.
This is a long comment text.
This is a long comment text. This is a long comment text.
</div>
<script type="text/javascript">
$(document).ready(function() {
$(".comment").shorten();
});
</script>
Code language: HTML, XML (xml)
You may want to pass the parameters to shorten() method and override the default ones. $(".comment").shorten({
"showChars" : 200
});
$(".comment").shorten({
"showChars" : 150,
"moreText" : "See More",
});
$(".comment").shorten({
"showChars" : 50,
"moreText" : "See More",
"lessText" : "Less",
});
Code language: JavaScript (javascript)
Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
Nice, although maybe you should think about making it a plugin instead to allow reuse and configuration options. Something like http://pastie.org/1416794 (your code as a plugin, with reservations for jQuery-plugin-best-practise-abuse...)
@Jay - fantastic :) I will update the post with jQuery plugin information. Thanks for that.
Hi, I noticed that whenever there's a ... and you click "More", there's a gap between the character, which would split up a whole word. Is there any way to fix that? Thanks!
@karly - wow! thanks for pointing out the space issue. I have updated the script. Thanks again. Have a look :)
What awesome site!!!!!!!!! Help me very much on the form enhancement!! Thanks guys!!!
Yes good
Nice work -> The only change I had to make to this was to make sure that the split doesn't occur within an HTML tag. For example, if you had some text that was wrapped with a "strong" tag, and the split ends up being in between the "o" and the "n" in the html tag, that tag would be broken. I changed it so that the code looks for a closing bracket (">") before an opening bracket ("<") in the 'remainder text'. If it is, it adds one to the number of characters to show & tries again.
Hey Jason any chance you could share your solution for not breaking html? Thanks.
I am also facing the same problem, if there are html tags, more link doesn't show at all. is there any way we can solve this issue. kindly please reply me.
Hey Jason, could you share that code update with us...facing the same issue!!!
Anybody managed to find/write what Jason is talking about?
Would really appreciate the implemented code. :)
Thanks.
g
Please share code, as I am facing same issue
Thank you
Mehak
Thanks for the code works pretty great for me.
I was wondering if anyone has/knows the best way to modify the function to hide a previously shown section when a new section has been clicked.
thanks again for the example!
In your three examples it doubles the letter where the eclipse sits
so instead of displaying the correct word 'sagittis' it display 'saagittis'.
Is there a quick way to fix this?
move the and remove the -1 from showChar
var c = content.substr(0, showChar);
var h = content.substr(showChar, content.length - showChar);
var html = c + ''+ellipsestext+'' + h + ' '+moretext+'';
Also may I see an example using the plug-in as this does not seem to work for me.
Great work by the way.
Thanks a lot man.
nice plugin...
Is there any option to customize the style of the more/less text with css..
i don't the read more or less text as a standard link..
Patel,
works great so thank you! I have one possible request: is it possible to save /show the break lines (br) ?? Looking at the source it shows correctly:
line one
line two
line three
but in display I get: Line one. Line two. Line three.
Thanks