...

View Full Version : Is there any way to wrap text in a textarea in FF?



shedokan
06-01-2009, 07:09 PM
Is there any way that I can make a textarea wrap in firefox?
I've tried this:



calculateLines: function(text){
text = text.split('\n').join('');

temp = '';
chcount = tchs = c= 0;
do {
ch = text.substring(c, c+1); // first character
if(ch == '\n'){ // if character is a hard return
temp += ch;
chcount = 1;
} else if(chcount == 9) { // line has max chacters on this line
temp += '\n' + ch; // go to next line
chcount = 1; // reset chcount
} else {// Not a newline or max characters ...
temp += ch;
chcount++; // so add 1 to chcount
}
} while(++c<text.length);
return temp;
}
updateInput: function(){
val=this.textarea.val();
if($('textarea',this.name).length==0){
return false;
}
lastval=val;
if(!val){
this.textarea.height(14);
return false;
}
input=this.textarea[0];

if(document.selection) {
r = document.selection.createRange();
re = input.createTextRange();
rc = re.duplicate();
re.moveToBookmark(r.getBookmark());
rc.setEndPoint('EndToStart', re);

lastsel = rc.text.length;
} else if(input.selectionStart || input.selectionStart == '0'){
lastsel = input.selectionStart;
}
toomuch=false;
if(val.length>199){
val=this.textarea.data('val');
this.textarea.val(val);
--lastsel;
toomuch=true;
}

text=this.calculateLines(val);
ns=text.split('\n').length;
if(this.textarea.data('ns')!=ns){
this.textarea.height( ns*14 ).data('ns',ns);
}

if(!toomuch && text!=lastval){
this.textarea.val(text);

tmp=text.split('\n').join('');
if(tmp!=lastval && tmp.length<=199 || ns-1==1){
if(Math.round(lastsel/10)*10==lastsel){
++lastsel;
}
this.textarea.data('val',text.split('\n').join(''));
}
}
if(!document.selection){
input.selectionStart=input.selectionEnd=lastsel;
} else {
var selRange = input.createTextRange();
selRange.collapse(true);
selRange.moveStart("character", lastsel);
selRange.moveEnd("character", 0);
selRange.select();
input.focus();
}
}
I am calling updateInput with this:
setInterval(function() { self.updateInput() }, 10);

it's kinda not understandable but maybe someone can figure it out.

thanks.

Eldarrion
06-01-2009, 07:23 PM
What you're looking for is the WRAP attribute for textareas. Usually a value of "soft", "hard" or "off", though it won't split a word in two to wrap in the textarea, so a word that is longer than the displayed columns (cols attribute) will not wrap. Try, see how it works for you.

rnd me
06-01-2009, 10:47 PM
String.prototype.lineWrap=function (colLen, delm) {
if (!delm) {
delm = "-\n";
}
if (!colLen) {
colLen = this.length;
}
var bs = "";
var mx = this.length;
for (i = 0; i < mx; i += colLen) {
bs += this.slice(i, i + colLen) + delm;
}
return bs.slice(0, (bs.length - delm.length));
}


arg1 is the # of chars per line
arg2 (optional )is the delimiter to use between lines, defaults to "-\n"...


example:


"Hello world, this is JavaScript".wrap(11);
results:


Hello world-
, this is J-
avaScript

shedokan
06-01-2009, 10:48 PM
That's the problem I want to split a long word, because it is used for an OS files rename system.

shedokan
06-02-2009, 02:04 AM
rnd me:
Sorry didn't see that before I posted.

there is one major problem with your code, and that is that if the user is editing the text and it updates the textarea, the editing poointer goes to the end.

that's why I'm saving it and placing it back in place.

rnd me
06-02-2009, 09:42 AM
rnd me:
Sorry didn't see that before I posted.

there is one major problem with your code, and that is that if the user is editing the text and it updates the textarea, the editing poointer goes to the end.

that's why I'm saving it and placing it back in place.

you could backup the cursor loc just before, and re-apply it just after.

in firefox it's as simple as

var oldPos = txt.selectionStart.
// do you stuff
txt.selectionStart=txt.selectionEnd=oldPos;



you might want to also backup and restore txt.scrollTop: the vertical scroll position.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum