Hello
Im looking for a function that will allow me limit number of characters in li tag.
Tried to do it that way:
$('li.latestnewsfooterCol').limit('5');
but this dont seem to work.
Can anyone help me please?
thank you in advance.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Part 1: Limiting number of characters
This will go through each selected
liand reduce the contents to 5 character using the.slice()method.Slice extracts up to, but not including the end slice.
jsFiddle example
To make it into a function:
jsFiddle example
Call the above with a jQuery object like,
limit($('li.latestnewsfooterCol'), 5);I use
.each()in both cases in case you have more than one element selected.Part 2: Limiting words
Word count are a little more complicated. Take a look here. Fortunately it’s much compacter on jQuery:
jsFiddle example
First the text is split into an array by whitespace,
$this.text().split(/\t+/). Then the array is sliced to the right length and rejoined with spaces,.slice(0,num).join(" ")Note that this might replace tabs or line breaks with spaces.As a note, consider that this will be only available to users with Javascript enabled.