Many time we are displaying text in javascript in the limit area where we want to break the string into multiple line. Recently I got requirement, so I created this function which helps to solve the problem
| 01 | /* |
| 02 | * param |
| 03 | * d string to be splited in multiple line |
| 04 | * l length where to break |
| 05 | */ |
| 06 | function splitmultiline(d, l){ |
| 07 | var df = "" |
| 08 | var dl = 0 |
| 09 | if(d!=undefined){ |
| 10 | var dlist = d.split(" "); |
| 11 | for(i = 0; i < dlist.length; i++){ |
| 12 | dl +=dlist[i].length |
| 13 | if(i!=0) |
| 14 | df+=" " |
| 15 | if(dl>=l){ |
| 16 | df+="<br>" |
| 17 | l +=l; |
| 18 | } |
| 19 | df+=dlist[i]; |
| 20 | } |
| 21 | } |
| 22 | return df; |
| 23 | } |
usage
| 1 | var str = "This is my test for spliting into multi line" |
| 2 | document.write(splitmultiline(str, 25)) |
| 3 | //output will be This is my test for spliting<br>into multi line |
Post a Comment