I have a scrolling text box and after every 60 characters I want to add a * ... How can I do this using javascript
Printable View
I have a scrolling text box and after every 60 characters I want to add a * ... How can I do this using javascript
Define "scrolling text box".
Is the user entering data or is the data already in the box?
wohoo, first post after a long holiday in Spain.
get all the text which is scrolling into a variable called text_which_scrolls
then try this untested code:
Code:new_text=""
j=0
for (i=60; i<text_which_scrolls.length; i=i+60)
{
new_text += text_which_scrolls.substring(j,i)+"*"
j=i
}
text_which_scrolls = new_text
a scrolling text box is a text box with scroll bars...
<textarea rows="2" name="S1" cols="20"></textarea>
I've done it but I'm not hapyy with it, it only works in IE and I don't know why. Can some one please tell me why?
Here's the code:
Code:<!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>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript">
function doIt() {
text_which_scrolls = document.getElementById('wee').innerHTML
new_text=""
i= new Number(0)
while (i<text_which_scrolls.length)
{
new_text += text_which_scrolls.substring(i,parseInt(i)+60)+"*"
i=i+60
}
alert(new_text)
document.getElementById('wee').innerHTML = new_text
}
</script>
</head>
<body onload="doIt()">
<textarea cols="20" rows="10" id="wee">
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
</textarea>
</body>
</html>
This is good. As I asked the * is placed at the end of the 60th character. This may cut off a letter in a word. Is it possible to put a * at the end of the last word before 60 characters?
you do want it to insert a character right? not replace the 60th char with a *.
I could make it reaplce the nearest space which is behind every 60th char. Is that ok?
That would be perfect..
sorry but I don't have time to write this, maybe someone else can. Basically you need to get the substring of the 60 char chunk. then do a lastIndexOf(" ") of it, then replace that with a *. not hard. I just don't have time.
Use the value property to refer to the contents of the text area.