Click here to Skip to main content
15,916,188 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do i break a line in paragraph when it reaches 100 characters using by Javascript

What I have tried:

var getst = document.getElementById("para").value;
if(getst.length==100)
getst.replace(/
/g);
Posted
Updated 25-May-16 1:14am

1 solution

try this


HTML
<p id="para1"> Your text </p>

<script>
    var text = document.getElementById('para1').innerText;
    var newText = '';
    var count = 0;
    for (var i = 0; i < text.length; i++) {
        newText += text[i];
        count++;
        if (count == 100) {
            count = 0;
            newText += '<br />';
        }
    }
    document.getElementById('para1').innerHTML = newText;
</script>
 
Share this answer
 
v2
Comments
Prasath 1992 25-May-16 7:21am    
Thank You boss.. Its working
Karthik_Mahalingam 25-May-16 7:22am    
welcome,
but you can optimize it using substring, just explore, its your homework :)
Prasath 1992 25-May-16 7:24am    
Yes.. I'm doing now..
Karthik_Mahalingam 25-May-16 7:25am    
ok.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900