Reputation:
I am having a long text inside p tag.
<p> type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into
electronic typesetting, remaining essentially unchanged. It was popularised
in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</P>
i want to show a read more link if the text is greater than 100 charectors(like in whatsapp).How to implement that? whether i can do it by pur css? or js needed? it should toggle between readmore and readless.
Upvotes: 1
Views: 215
Reputation: 262
try this answer
htmt:
<p id="text"></p>
javascript:
var text = "type and scrambled it to make a type specimen book It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum";
var text2 = '';
if(text.length>100){
var textArray = new Array();
textArray = text.split("");
for(var i=0;i<textArray.length;i++){
text2 += textArray[i];
if(i==99){
text2 += "<a id='readmore' href='#more' onclick='document.getElementById(\"more\").style.display=\"inline\";document.getElementById(\"readmore\").style.display=\"none\"'>readmore</a>";
text2 += "<span name='more' style='display:none' id='more'>";
}
if(i==textArray.length-1){
text2 += "</span>";
}
}
}else {
text2 = text;
}
document.getElementById("text").innerHTML = text2;
Upvotes: 0
Reputation: 2660
Try below code i think this is helpful for you:
HTML:
<p class="minimize">It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text.</p>
<p class="minimize">It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text.</p>
JS:
jQuery(function(){
var minimized_elements = $('p.minimize');
minimized_elements.each(function(){
var t = $(this).text();
if(t.length < 100) return;
$(this).html(
t.slice(0,100)+'<span>... </span><a href="#" class="more">More</a>'+
'<span style="display:none;">'+ t.slice(100,t.length)+' <a href="#" class="less">Less</a></span>'
);
});
$('a.more', minimized_elements).click(function(event){
event.preventDefault();
$(this).hide().prev().hide();
$(this).next().show();
});
$('a.less', minimized_elements).click(function(event){
event.preventDefault();
$(this).parent().hide().prev().show().prev().show();
});
});
See FIDDLE DEMO
See ReFerance Link
Upvotes: 1