473,806 Members | 2,332 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.src Element;
if (taObj.value.le ngth==taObj.max Length*1) return false;
}
function taCount(visCnt) {
var taObj=event.src Element;
if (taObj.value.le ngth>taObj.maxL ength*1)
taObj.value=taO bj.value.substr ing(0,taObj.max Length*1);
if (visCnt) visCnt.innerTex t=taObj.maxLeng th-taObj.value.len gth;
}

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

3) Textarea code
<textarea cols=40 rows=7 wrap=virtual onkeypress="ret urn taLimit()"
onkeyup="return taCount(myCount er)" 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 4502

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('m axLength');
>
These are the three parts of the working code

1) Javascript

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

function taLimit( taObj ) {
if (taObj.value.le ngth==taObj.max Length*1) return false;
or:

return !(taObj.value.l ength == taObj.getAttrib ute('maxlength' ));
}
function taCount(visCnt) {
var taObj=event.src Element;
Again, pass a reference to the element using "this", and get maxLength
only once:

function taCount(taObj, visCnt) {
var maxLength = taObj.getAttrib ute('maxLength' );
if (taObj.value.le ngth>taObj.maxL ength*1)
if (taObj.value.le ngth maxLength)
taObj.value=taO bj.value.substr ing(0,taObj.max Length*1);
There is no need for the *1 bit:

taObj.value=taO bj.value.substr ing(0, maxLength);

and so on...
if (visCnt) visCnt.innerTex t=taObj.maxLeng th-taObj.value.len gth;
Replace innerText with innerHTML, use document.getEle mentById(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>25 5</spanremaining
chars

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

Also, cols is a mandatory attribute.
onkeyup="return taCount(myCount er)" 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
4069
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" cols="35"></textarea> </span> The textarea is filled up with the complete rest of the xsl code. When I put in a sign like this - everything works and the sign is shown in the textarea.
4
5277
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 when you exceed the four lines (rows=4) the scrollbar becomes active. That moment..the moment the scrollbar becomes active..can javascript recognize/capture this moment? Thanks, Simon
11
13732
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, there's an "Edit" link. So the page itself looks something like this: <HTML><HEAD><TITLE>blah</TITLE></HEAD><BODY> <!-- TEXT STARTS HERE --> <H1>Hello World!</H1> <P>More stuff here...</P>
4
4263
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
5994
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 think my way around. I am using a <textarea> to display the code but some of my scripts have <textarea> tags in them and when the </textarea> tag is entered, it closes my <textarea> and the rest of the code ends up outside of the textarea.
16
2857
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 criteria and at the bottom of this list i have a button that inserts all these records to a´nother table in the database. So long everything's ok. BUT, at the top of this list I have a textarea that the user can write down some text to be put...
3
5680
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 replace spaces with &nbsp;. The <br> part works well enough, but I keep getting "%20" instead of "&nbsp;" for the spaces. I understand that escape() changes " " to "%20", but I would think the ConvertSpaces function below would change the %20 to...
5
4785
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 test="@READONLY"> <xsl:attribute name="READONLY"><xsl:text>READONLY</xsl:text></ xsl:attribute> </xsl:if> <xsl:value-of select="."/> </TEXTAREA>
3
2333
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 that when i edit the text in the text Area it isn't saving it on the textfile. What do i have wrong? Thanks a lot. import java.awt.BorderLayout; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import...
0
9597
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10366
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4329
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3850
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.