tags:

views:

27

answers:

2

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.

+1  A: 

Hi,

Can you please be more specific? Do you want to have something like

...
<li>A very long list item</li>
...
<script type="text/javascript">
    function limit(no_of_chars) {

    }
    limit(5);
</script>

to output

A ver

?

Claudiu
Yes. I have links in my footer that simply dont fit. Just want to limit them to number of characters for that particular tag
Dom
@Dom, you should probably do something like that server-side. Do you want your design to break if users have Javascript disabled, or use either an old browser or a newly-released browser that your jQuery script doesn't support? Edit: also, 5 characters is... pretty small.
eyelidlessness
The problem is that I am using it for joomla. If i would wanted to do it on server site would affect entire module on whole site. it is a latest news feed that shows on footer and LHS. Only on footer i need to limit numb of characters. I know that it might break for older browsers. although it will not affect look and feel too bad, so I can cope with it. Thank you for your advice.
Dom
+1  A: 

Part 1: Limiting number of characters


This will go through each selected li and reduce the contents to 5 character using the .slice() method.

Slice extracts up to, but not including the end slice.

$('li.latestnewsfooterCol').each(function() {
    var $this = $(this);
    $this.text( $this.text().slice(0,5) );
});

jsFiddle example


To make it into a function:

var maxNum = function($elie, num) {
    var $this;
    $elie.each(function() {
        $this = $(this);
        $this.text( $this.text().slice(0,num) );
    });
}

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:

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);
});

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.


Peter Ajtai
just a small correction `var $this = $(this)`
Ninja Dude
@Avinash - Thanks!
Peter Ajtai
This is great! But is there a way that I can count words in similar way? Sorry for messing up
Dom
@Dom - Edited in word count. Arrays also have a `.slice()` method.
Peter Ajtai
Works like a charm. thank you so much!
Dom
@Dom - You're welcome ;)
Peter Ajtai