Forum Moderators: open

Message Too Old, No Replies

Textarea Element Size?

textarea element size

         

The_Warden

9:33 pm on Aug 15, 2002 (gmt 0)

10+ Year Member



Is there a way to set the size of the TEXTAREA element by an exact amount of characters? I realize you can set the amount of ROWS and COLS but what if you want to be more specific? I would assume there is no why to achieve this with HTML. Could I do this with CSS? Or is my only direction in JavaScript or a server-side scripting langage like PHP?

Thanks!

NeedScripts

9:49 pm on Aug 15, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



do you mean, allowing users to *input* only specified number of characters?

If yes, then it can be done through JavaScripts :) Below is the code

### Code between <head> </head>
<script language = "Javascript">

function taLimit() {
var taObj=event.srcElement;
if (taObj.value.length==taObj.maxLength*1) return false;
}

function taCount(visCnt) {
var taObj=event.srcElement;
if (taObj.value.length>taObj.maxLength*1) taObj.value=taObj.value.substring(0,taObj.maxLength*1);
if (visCnt) visCnt.innerText=taObj.maxLength-taObj.value.length;
}
</script>

#######

<textarea rows="7" name="TEXTAREA-NAME" onkeypress="return taLimit()" onkeyup="return taCount(myCounter)" maxLength="2000" cols="38"></textarea><br>

<font size="2" face="Arial">You have <font color="#CC0000"> <B><SPAN id=myCounter>
2000</SPAN></B></font> characters remaining ...</font> </font>

##############

I am sure you got the drill, on how it works. Also, you can use same <head> code for multiple use within the same page.

There might be some better ways out there, but I am only aware of this one. :(

Hope this helps :)

Purple Martin

1:11 am on Aug 16, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just yesterday I was setting the width of text and select elements to exact pixel widths with css, in the same way as you would for a div. It might work for textarea elements too!

Brett_Tabke

1:33 am on Aug 16, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



But css in forms is not garanteed compatable with various browsers. There is a huge disagreement about forms and css in the browser communities and css communities.

If you put up something browser specific, just make sure it isn't mission critical.

Have heard rumors that the recent betas of ie even allow users to set their own font size in form elements.

Purple Martin

3:29 am on Aug 16, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can already set font sizes in IE5 for select elements, but not for text elements.

starway

10:57 am on Aug 16, 2002 (gmt 0)

10+ Year Member



>>But css in forms is not garanteed compatable with various browsers.

Why not? For example, this:

.one4all { font: 8pt verdana,arial,sans-serif; color: gold; width: 100px }

applied to different form elements like <input type=text>, <textarea> or <select> will set their text and width exactly the same in Opera, IE and Mozilla on Win (I can't test on other OS like Mac, for example). The only exception is that Opera will not change the text color.

In order to make width of an element look the same in NN4, you have to use a combination of font settings in CSS and "size" attribute of an <input> tag (or "cols" attribute of <textarea>) - just play with it a little and you'll get it looking almost or exactly the same as in other browsers.

The_Warden

2:47 pm on Aug 22, 2002 (gmt 0)

10+ Year Member



Thanks to everyone for the wonderful response. CSS is a great route but correct me if I'm wrong, CSS is not fully compatible with Netscape Navigator 4.x and IE4 for this type of feature?

NeedsScripts
Thanks, wonderful script. I actually found one similar to this on Dynamic Drive web site. My question is now can I get this script to function in Netscape Navigator 4.x and IE4? I realize this type of script works fine in the latest browsers but my user base unfortunately is behind the times. Here's the code I used...

<SCRIPT TYPE="text/javascript">
/*
Form field Limiter script- By Dynamic Drive
For full source code and more DHTML scripts, visit [dynamicdrive.com...]
This credit MUST stay intact for use
*/

var ns6=document.getElementById&&!document.all

function restrictinput(maxlength,e,placeholder){
if (window.event&&event.srcElement.value.length>=maxlength)
return false
else if (e.target&&e.target==eval(placeholder)&&e.target.value.length>=maxlength){
var pressedkey=/[a-zA-Z0-9\.\,\/]/ //detect alphanumeric keys
if (pressedkey.test(String.fromCharCode(e.which)))
e.stopPropagation()
}
}

function countlimit(maxlength,e,placeholder){
var theform=eval(placeholder)
var lengthleft=maxlength-theform.value.length
var placeholderobj=document.all? document.all[placeholder] : document.getElementById(placeholder)
if (window.event¦¦e.target&&e.target==eval(placeholder)){
if (lengthleft<0)
theform.value=theform.value.substring(0,maxlength)
placeholderobj.innerHTML=lengthleft
}
}

function displaylimit(theform,thelimit){
var limit_text='<b><span id="'+theform.toString()+'">'+thelimit+'</span></b> characters remaining on your input limit'
if (document.all¦¦ns6)
document.write(limit_text)
if (document.all){
eval(theform).onkeypress=function(){ return restrictinput(thelimit,event,theform)}
eval(theform).onkeyup=function(){ countlimit(thelimit,event,theform)}
}
else if (ns6){
document.body.addEventListener('keypress', function(event) { restrictinput(thelimit,event,theform) }, true);
document.body.addEventListener('keyup', function(event) { countlimit(thelimit,event,theform) }, true);
}
}

</SCRIPT>

And the script to put along with the form element...

<SCRIPT>displaylimit("document.form.szComments",255)</SCRIPT>

NeedScripts

3:13 pm on Aug 22, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nope as far as I know it won't work with Netscape 4.x, but most good things don't work on Netscape anyway ;)

About CSS, I think if you are using TopStyle [I love it, it is very good and I would say best css editor ever made], it will tell you if your CSS will work with Netscape 4 or other browers or not.

The_Warden

3:20 pm on Aug 22, 2002 (gmt 0)

10+ Year Member



NeedScripts
I'm aware it doesn't work in Netscape 4.x... if you use Netscape 6+/Mozilla 1.0+ it works great. So are you saying you do not know if it would be possible to modify the javascript to have it work Netscape 4.x?

Okay... don't use that program but I'll see about looking into it.

Thanks!

starway

6:18 am on Aug 23, 2002 (gmt 0)

10+ Year Member



I always wonder why people are trying a hard ways to solve a problem, while there are much more simple ones...

The_Warden, you can forget about your problems using one simple CSS line!

The_Warden

1:26 pm on Aug 27, 2002 (gmt 0)

10+ Year Member



starway
I'm not trying to go the hard way. I'm just trying to find a solution to the problem.

One line of CSS will solve my problem.. geez man what is it :-)?? I'll see if I can find it in the CSS 2 Specifications.

I was just initially worried about using CSS because I figured it wouldn't work in Netscape 4.x.

starway

6:41 am on Aug 28, 2002 (gmt 0)

10+ Year Member



Did you read my first post to this thread? I guess you didin't.
Thast's bad, because I posted an exact solution that I'm usign for a couple of years.

The_Warden

7:24 pm on Aug 29, 2002 (gmt 0)

10+ Year Member



starway
I tried what you suggested with CSS and it doesn't work for me. NN4 doesn't even recongize it. Is there something else you are doing? I can't understand how that line of CSS code would set the limit of a TEXTAREA element maximum characters allowed. With the CSS line you gave me that just sets the font face, font color and width the of element. Unless I'm way out in left field here I'm not understanding how this would work.

rewboss

6:24 am on Aug 30, 2002 (gmt 0)

10+ Year Member



(Oops! -- Misunderstood the question)

starway

7:04 am on Aug 30, 2002 (gmt 0)

10+ Year Member



Oh, seems I really did!
Sorry, seems like I skipped your first post, and paid attention to those ones that talk about input fields width.
Of course, you cannot limit amount of characters with CSS! I gave an example how you can make form fields have the same width in any browser.