DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   JavaScript / DHTML / AJAX (http://www.daniweb.com/forums/forum117.html)
-   -   limit no of char per line (http://www.daniweb.com/forums/thread160228.html)

queenc Dec 1st, 2008 6:54 am
limit no of char per line
 
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

Luckychap Dec 1st, 2008 9:11 am
Re: limit no of char per line
 
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++;
    }
}

queenc Dec 2nd, 2008 3:02 am
Re: limit no of char per line
 
this is not working

Luckychap Dec 2nd, 2008 3:24 am
Re: limit no of char per line
 
What have you done till now?

Luckychap Dec 2nd, 2008 3:56 am
Re: limit no of char per line
 
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.

queenc Dec 2nd, 2008 4:38 am
Re: limit no of char per line
 
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>

queenc Dec 2nd, 2008 4:41 am
Re: limit no of char per line
 
in the function you have passed a variable called 'e'...wat is tat for

Luckychap Dec 2nd, 2008 6:11 am
Re: limit no of char per line
 
Where you had set the id for textarea?

ignore e in the function

queenc Dec 3rd, 2008 1:47 am
Re: limit no of char per line
 
i want it to work when the tries to paste also


All times are GMT -4. The time now is 4:31 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC