I have an div with same class in a page, I have to go through each element in that div and see the text length, if the text length exceeds suppose 300. I have to hide/remove other elements and show the remaining elements.
var x = 0
$(".updatedText").each(function(index, item) {
var self = $(this);
self.children().each(function(index, item){
// self; parent li
// this; child li
//console.log( index + ": " + $(this).text());
//console.log(isLastElement);
if(x >= 100) {
//$(this).text("");
var y = x - 100;
$(item).prev().text($(item).prev().text().substring(0,y));
$(item).prev().append("....");
$(this).remove();
if(index == 0) {
x = 0;
}
} else {
x = x + $(this).text().length;
}
});
});
Updated: I am able to limit the number of characters in each div without losing HTML elements. But I am not able to fix the number of characters each div should have.
Right now they are limiting the amount of text but the total text length varies. How can I make it exact 300 characters?
Note: I should not loose any HTML when limiting text. Updated Fiddle also.