For <input type="text" /> elements, you can just use the maxlength attribute.
For other elements, such as <textarea>, you can check on the element's onkeyup event.
$('.shortened')
.keyup(function(e){
var $this = $(this);
$this.text($this.text().substring(0, max)); //Set max to 40, or whatever you want
});
For other, non-form elements, you can do the same thing, but you don't have to do it on an event handler.
$('.shortened')
.each(function(){
var remainTxt = $(this).text().substring(max, $(this).text().length);
$(this).text($(this).text().substring(0,max));
});
Edit: To match up with Mark's answer, you can store the remaining .text() substring and append that when the user clicks on an "expand" button.