So I have created the following code to deal with one instance of a string that requires truncating with expandable and collapsable click events.
$(document).ready(function () {
var Element = $("[class*='truncate']");
var FullText = $(".truncate").text();
var Show = "<span class='show'> [...]</span>";
var Hide = "<span class='hide'> [ ]</span>";
var shortString = FullText.substring(0, 5);
if (FullText.length > 5) {
$(Element).html(shortString + Show);
}
$(Element).on("click", ".show", function () {
$(Element).html(FullText + Hide);
});
$(Element).on("click", ".hide", function () {
$(Element).html(shortString + Show);
});
});
Which truncates the following HTML
<div class="truncate">MBK-40B-FRLBBLCSLWLHBLHSLSALSAS32PDL</div>
The above code works perfectly for one instance of the class .truncate
but when adding more things get a bit messy. How can I make this code work when there are many elements with this class, some of which will need to be truncated while others won't?