Best way to truncate an ordered breadcrumb list if beyond a certain char count
<p>I think I have the code, but I want to make sure it is the most robust, streamlined as it could be. Would appreciate any help at all.</p>

<p>Conditions:</p>

<ul>
<li>Total breadcrumb count cannot be more than 50</li>
<li>Total char count of item 3 cannnot exceed 20</li>
<li>Only item 3 can be truncated for conditions #1 and #2 to be met and it must be followed by "..." after truncation and THAT "..." will also count towards the total.</li>
</ul>

<p>So, here is what a list would look like delivered dynamically to the page before any truncation.-- btw. I will be using jQuery.</p>

<p>For condition 1:</p>

<pre><code>&lt;ol id="oldiv"&gt;
&lt;li&gt;item 1&lt;/li&gt;
&lt;li&gt;item 2 more&lt;/li&gt;
&lt;li id="item3"&gt;item 3 some more&lt;/li&gt;
&lt;li&gt;item 4 some more chars and/li&gt;
&lt;/ol&gt;
</code></pre>

<p>For condition 2</p>

<pre><code>&lt;ol id="oldiv"&gt;
&lt;li&gt;item 1&lt;/li&gt;
&lt;li&gt;item 2 more&lt;/li&gt;
&lt;li id="item3"&gt;item 3 some more and a little more and more still and more&lt;/li&gt;
&lt;li&gt;item 4/li&gt;
&lt;/ol&gt;
</code></pre>

<p>So I am figuring something like.</p>

<pre><code>var ttl_ol_chars = $('#oldiv').text().length;
var ttl_item3_char = $('#item3').text().length;
var item3txt = $(s).text();
if(ttl_ol_chars &gt; 50){
    item3txt = item3txt.substring(0, (ttl_item3_char - (ttl_ol_chars - (47 - 3)))) + "...";
    $('#item3').text(item3txt);
}
else if(ttl_item3_char &gt; 20){
    item3txt = item3txt.substring(0, (ttl_item3_char - 3)) + "...";
    $('#item3').text(item3txt);       
}
</code></pre>

<p>You might know the answer to this, but I notice that unless I do an EXTRA 3 for the over 50 count one, my total count is always off. I just assume to do 50 - 3, because that extra "..." will be the difference. But it always seems 3 more off. Anyways.</p>

<p>The code looks extraneous to me. Should I put a helper method in there that does the figuring? btw, this all happens in an anonymous function. So something like this?</p>

<pre><code>function(){
_truncateString(x){
    item3txt = item3txt.substring(0, x) + "...";
    $('#item3').text(item3txt); 
}

if(ttl_ol_chars &gt; 50){
    var trimCount = ttl_item3_char - (ttl_ol_chars - (47 - 3));
    _truncateString(trimCount);
}
else if(ttl_item3_char &gt; 20){
    _truncateString(17);     
}
}();
</code></pre>

<p>Thanks a bunch all.</p>
