Click here to Skip to main content
15,888,979 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i use
onkeyup
function to type value in div to textbox. but now i also add break after 10 character..

What I have tried:

div.innerText.replace(/(.{10})/g, "$1<br>")


i use this but it is only work in alert not in division value....




and this is my all code


<input id='demoInput1' onkeyup="demo1()" />

<div id='demoDiv1' class='resizable'></div>




<script>


function demo1() {
	
    var div = document.getElementById('demoDiv1');
    var input = document.getElementById('demoInput1');
	
 	div.innerText = input.value;
	
    resizeContents();
	
	var content = input.value;  

	alert(div.innerText.replace(/(.{10})/g, "$1<br>"));

}




function resizeContents() {
	
    var resizableDivs = document.getElementsByClassName('resizable');
	
    for (index = 0; index < resizableDivs.length; ++index) {
        var div = resizableDivs[index];
        if (div.innerText != '') {
            var divWidth = div.clientWidth;
            var divHeight = div.clientHeight;

            var contentWidth = div.scrollWidth;
            var contentHeight = div.scrollHeight;

            var fontSize = div.style.fontSize;
            fontSize = Number(fontSize.substring(0, fontSize.length - 2));

            while (contentWidth <= divWidth && contentHeight <= divHeight) {
                fontSize += 1;
                div.style.fontSize = fontSize + 'px';
                contentWidth = div.scrollWidth;
                contentHeight = div.scrollHeight;
				
            }

             while (contentWidth > divWidth || contentHeight > divHeight) {
                 fontSize -= 1;
                 div.style.fontSize = fontSize + 'px';

                 contentWidth = div.scrollWidth;
                 contentHeight = div.scrollHeight;
             }
        }
    }
	
	
}

</script>
Posted
Updated 4-Jun-19 2:46am

1 solution

If it shows up fine in your alert but not in your div, then the problem lies in how you put the value in the div. You are not showing us how you're doing that so we can't see why it doesn't work, but this line should do the job:
JavaScript
div.innerHTML = input.value.replace(/(.{10})/g, "$1<br>");
 
Share this answer
 
v2
Comments
[no name] 18-Jun-19 10:52am    
Thanks!

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