473,385 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Textarea code

I have been using this code for sometime. I can use it to show the user how
many remaining characters are reamaining in a text area. Now I need to
develop the code and dont know where to start.

These are the three parts of the working code

1) 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.maxLengt h*1);
if (visCnt) visCnt.innerText=taObj.maxLength-taObj.value.length;
}

2) Live output to user
You have max 255 chars. You have <span id=myCounter>255</spanremaining
chars

3) Textarea code
<textarea cols=40 rows=7 wrap=virtual onkeypress="return taLimit()"
onkeyup="return taCount(myCounter)" maxLength="255"></textarea>

The problem is that now I need to show the user some existing text
(contained in a variable) in the textarea which he/she can edit or add to.
The initial number is the number of remaining characters shown by my code is
a static 255. If 120 characters are left and one is added it changes from
255 remaining to 119. (119 is correct).

When the textarea is initially displays I would like it to subtract the
number of characters contained in the variable from "maxlength" and display
the true ammount of remaining characters.

Can anyone point me in the right direction?

Garry Jones
Sweden
Oct 10 '06 #1
1 4473

Garry Jones wrote:
I have been using this code for sometime. I can use it to show the user how
many remaining characters are reamaining in a text area. Now I need to
develop the code and dont know where to start.
Here's some (untested) fixes - your code seems very IE-specific. In
general, pass a reference to the textarea from the event handler using
"this". Replace direct calls to the (invalid) maxlength attribute to
ones that use getAttribute('maxLength');
>
These are the three parts of the working code

1) Javascript

function taLimit() {
var taObj=event.srcElement;
Pass a reference to the element to the function using 'this', then
ditch the (IE proprietary) event.srcElement line:

function taLimit( taObj ) {
if (taObj.value.length==taObj.maxLength*1) return false;
or:

return !(taObj.value.length == taObj.getAttribute('maxlength'));
}
function taCount(visCnt) {
var taObj=event.srcElement;
Again, pass a reference to the element using "this", and get maxLength
only once:

function taCount(taObj, visCnt) {
var maxLength = taObj.getAttribute('maxLength');
if (taObj.value.length>taObj.maxLength*1)
if (taObj.value.length maxLength)
taObj.value=taObj.value.substring(0,taObj.maxLengt h*1);
There is no need for the *1 bit:

taObj.value=taObj.value.substring(0, maxLength);

and so on...
if (visCnt) visCnt.innerText=taObj.maxLength-taObj.value.length;
Replace innerText with innerHTML, use document.getElementById(visCnt)
rather than a direct call using the ID as a global variable.
}

2) Live output to user
You have max 255 chars. You have <span id=myCounter>255</spanremaining
chars

3) Textarea code
<textarea cols=40 rows=7 wrap=virtual onkeypress="return taLimit()"
<textarea cols=40 rows=7 wrap=virtual onkeypress="return
taLimit(this)"

Also, cols is a mandatory attribute.
onkeyup="return taCount(myCounter)" maxLength="255"></textarea>
onkeyup="return taCount(this, 'myCounter')"
maxLength="255"></textarea>

>
The problem is that now I need to show the user some existing text
(contained in a variable) in the textarea which he/she can edit or add to.
The initial number is the number of remaining characters shown by my code is
a static 255. If 120 characters are left and one is added it changes from
255 remaining to 119. (119 is correct).
Then call the appropriate functions onload.

You might want to investigate a function that runs onload, checks all
the textarea elements and for those that have a maxlength attribute,
add the appropriate onkey functions and then call them so that the
"characters left" message is accurate.

Incidentally, maxlength is not a valid attribute for textarea elements,
rows and cols attributes are mandatory. HTML validators will complain
- but that may not bother you.
--
Rob

Oct 11 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Thomas S. | last post by:
Hello I´ve got a question concerning the usage of a textarea in a xsl. When i just write the following code, <span style="font-size:12.0pt; "><textarea name="hotelbeschreibung" rows="5"...
4
by: Simon | last post by:
I have another question.. Let's take a textarea with the parameter: "rows=4" (<textarea rows=4 onkeydown="CheckScrollbar()"> ) If you start typing the scrollbar at the right is not active. Only...
11
by: Les Paul | last post by:
I'm trying to design an HTML page that can edit itself. In essence, it's just like a Wiki page, but my own very simple version. It's a page full of plain old HTML content, and then at the bottom,...
4
by: TJS | last post by:
can the rows and columns of a textarea element with an id be changed on the fly ? if so is there an example ?
6
by: wperry1 | last post by:
I am writing a small database utility to catalog all of my favorite ASM/JS/VBS... functions and scripts on an asp page. Everything is going smoothly except for one thing that I can't quite seem to...
16
by: Jen | last post by:
Hi. I have this problem that I think should be easy but have been struggling with this for days. I have a list based on a recordset from a database. This list consists of records meeting a certain...
3
by: FunkHouse9 | last post by:
I'm working on a form to collect data in a textarea which and am trying to keep returns and spaces. I have a couple of functions that I Frankensteined together to replace returns with <br> and to...
5
by: a113n | last post by:
I have the following XSL code to handle textareas: <!-- Match TEXTAREA --> <xsl:template name="TEXTAREA" match="TEXTAREA"> <TEXTAREA ROWS="{@ROWS}" COLS="{@COLS}" NAME="{@NAME}"> <xsl:if...
3
by: saytri | last post by:
I am displaying the contents of a textfile in a textArea. I want that when i edit this text in the textArea it is automatically saved in the textfile. I have wriitten this code, but the problem is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.