Truncate width function not working when passing integer
<p>I'm trying to create a universal  function that I can call from multiple places to truncate long text recursively to fit a predefined pixel width - using jquery.</p>

<p>Here is the code...</p>

<pre><code>function constrain(text, original, ideal_width){

 var ideal = parseInt(ideal_width);

 $('span.temp_item').remove();
 var temp_item = ('&lt;span class="temp_item" style="display:none"&gt;'+ text +'&lt;/span&gt;');
 var item_length = text.length; 
 $(temp_item).appendTo('body');
 var item_width = $('span.temp_item').width();

 if (item_width &gt; ideal) {
  var smaller_text = text.substr(0, (item_length-1));
  return constrain(smaller_text, original);
 } else if (item_length != original) {
  return (text + '&amp;hellip;');
 } else if (item_length == original) {
  return text;
 }
}
</code></pre>

<p>If I run the function like this:</p>

<pre><code>    $('.service_link span:odd').each(function(){
 var item_text = $(this).text();
 var original_length = item_text.length;
 var constrained = constrain(item_text, original_length,'175');
 $(this).html(constrained);
});
</code></pre>

<p>The text doesn't truncate. I also tried the 175 without the quotes.</p>

<p>If I define var ideal = 175; inside the function, then it works. Why is passing 175 to the function not working? I did a parseInt on it in case it was a string.</p>

<p>Also - this truncate code run a bit slow on older machines - any tips for speeding it up?</p>

<p>Thanks!</p>
