Part 1: character limit
This will go through each selected one liand reduce the content to 5 characters using . .slice()
Clippings of fragments before, but not including the final slice.
$('li.latestnewsfooterCol').each(function() {
var $this = $(this);
$this.text( $this.text().slice(0,5) );
});
To turn it into a function:
var maxNum = function($elie, num) {
var $this;
$elie.each(function() {
$this = $(this);
$this.text( $this.text().slice(0,num) );
});
}
Call above using jQuery object e.g. limit($('li.latestnewsfooterCol'), 5);
I use .each()in both cases if you have selected more than one item.
Part 2: Limit Words
. . , compacter jQuery:
var maxNum = function($elie, num) {
var $this;
$elie.each(function() {
$this = $(this);
$this.text( $this.text().split(/\s+/).slice(0,num).join(" ") );
});
}
$(function() {
maxNum($('li.latestnewsfooterCol'),5);
});
, $this.text().split(/\t+/). , .slice(0,num).join(" ") , .
, Javascript.