I was making read more-less toggle with jquery, which would appear after 100th symbol in text.
Right now i have code that does that with 100th symbol in html of OL tag. It works just like i want, but 100th symbol in markup is not the same as in text, because it counts tags too.
var showChar = 100;
var ellipsestext = "...";
var moretext = "Ещё";
var lesstext = "Скрыть";
$('.more').each(function () {
var content = $(this).html();
if (content.length > showChar) {
var c = content.substr(0, showChar);
var h = content.substr(showChar, content.length - showChar);
var position = h.indexOf('</li>');
var h = [h.slice(0, position), '</span></li><span class="morecontent">', h.slice(position + 5)].join('');
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);
$('.morecontent').each(function() {
$(this).css('display', 'none');
});
$('.moreellipses').css('display', 'inline');
} else {
$(this).addClass("less");
$(this).html(lesstext);
$('.morecontent').each(function () {
$(this).css('display', 'inline');
});
$('.moreellipses').css('display', 'none');
}
return false;
});
I tried to determine the position of 100th symbol in text in html, by getting substring from text, but substring might appear sooner in html, or even be broken by tags. Is there a way to modify my code, so that 'ellipsestext' would appear after 100th symbol in text?