javascript - Truncate Text in ul li anchor tag -


here html ,

<ul class="test">     <li><a href="#">python</a></li>     <li><a href="#">truncate should apply here </a></li>     <li><a href="#">c</a></li>     <li><a href="#">cpp</a></li>     <li><a href="#">java</a></li>     <li><a href="#">.net</a></li>     <li><a href="#">this long text in ul </a></li>     <li><a href="#">this long text in ul </a></li> </ul> 

i want truncate text in anchor tag , if length of text higher 6 chars.

the output should ,

    python     trun..     c     cpp     java     .net     this..     this.. 

how can using jquery ?

you can this:

$('ul.test li a').each(function() {     var text = $(this).text();     if(text.length > 6) {         $(this).text(text.substring(0, 4) + '..')     } }); 

fiddle demo


if want use pure css can use text-overflow: ellipsis property:

ul.test li {     width: 45px;     display: block;     overflow: hidden;     white-space: nowrap;     text-overflow: ellipsis; } 

however, cannot control number of limited characters before putting ellipsis using approach.

fiddle demo


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -