hi
i have a text area where i want to limit the number of charactersin each line.
for example if the user enters more than 50 caharacters it should automatically move the cursor to the next line
thanks

Recommended Answers

All 8 Replies

I do not know if there is a simple way. But at this moment here what you can do.

Write a event handler for onkeyup for your text area and count for every keypress if count exceeds 50 then append '\n'(new line character) by yourself.

thats it.
Your js keyhandler will look somthing like this:-

var count = 0;
function keypress (e) {
     if(count>=50) {
           document.getElementById('textarea_id').value = document.getElementById('textarea_id').value + '\n';
           count = 0;
     } else {
           count++;
     }
}

this is not working

What have you done till now?

Of course its working man. I tried it with document.getElementById('id of textarea')

What you want? That a working code is delivered to you so that you can sit back and rest.

tried something like this

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script language="javascript" type="text/javascript">
<!--
var count = 0;
function ress(){

if(count>=5) {
document.getElementById('textarea_id').value = document.getElementById('textarea_id').value + '\n';
count = 0;
} else {
count++;
}
}
//-->
</script>
</head>

<body>
<form>
<textarea name="textarea_id" onkeypress="ress();"></textarea>
</form>
</body>
</html>

in the function you have passed a variable called 'e'...wat is tat for

Where you had set the id for textarea?

ignore e in the function

i want it to work when the tries to paste also

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.