473,385 Members | 2,014 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.

Code Not Working in Firefox: keyup event

I have a web page with the following items on it:

[HTML]<label for="commissionschedule">
Commission Schedule<br />
<span id="charCounter">1000 characters or less</span>
</label>
<textarea id="commissionschedule" rows="10" cols="40"></textarea>[/HTML]

I'm using a standard javascript addEvent function and am attaching a keyup event to the textarea as follows:

Expand|Select|Wrap|Line Numbers
  1. function addEvent(elm, evType, fn, useCapture)
  2. // cross browser event handling
  3. {
  4.   if (elm.addEventListener)
  5.   {
  6.    elm.addEventListener(evType, fn, useCapture);
  7.    return true;
  8.   } else if (elm.attachEvent)
  9.   {
  10.    var r = elm.attachEvent('on' + evType, fn);
  11.    return r;
  12.   } else
  13.   {
  14.    elm['on' + evType] = fn;
  15.   }
  16. }
  17.  
  18. function addListeners(e)
  19. {
  20.   var commissionSchedule = document.getElementById("commissionschedule");
  21.   addEvent(commissionSchedule, 'keyup', setCounter, false);
  22. }
  23.  
  24. function setCounter() {
  25.     var charCounter = document.getElementById("charCounter");
  26.     var commissionSchedule = document.getElementById("commissionschedule");
  27.     var currentLength = commissionSchedule.value.length;
  28.     var numCharsLeft = 1000 - currentLength;
  29.     var htmlFrag = document.createTextNode("" + numCharsLeft + " characters left");
  30.     while (charCounter.hasChildNodes())
  31.         charCounter.removeChild(charCounter.lastChild);
  32.     charCounter.appendChild(htmlFrag);
  33. }
  34.  
  35. addEvent(window, 'load', addListeners, false);
Now, I've done this a dozen times before (with 'click', though, not 'keyup'), and never had a problem, but in this case, the setCounter function is never called in Firefox. In IE this works perfectly, updating #charCounter with the number of characters left on each keyup event. However, in FF, the setCounter event handler is never even called. I have checked, and setCounter IS getting attached to the onkeyup even handler for the textarea at page load, but never actually fires once the user begins typing.

Any idea? Thanks a ton!
Aug 17 '07 #1
1 4371
mrhoo
428 256MB
Nothing wrong with your script, its the html- firefox is stricter than IE.
A label can only contain its control element and text. Try this:

[HTML]<p >
<label for="commissionschedule">Commission Schedule</label>
<span id="charCounter">1000 characters or less</span>
</p>
<textarea id="commissionschedule" rows="10" cols="40"></textarea>[/HTML]
Aug 18 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: CJack | last post by:
hy, I have an mdi application, i create a child form and I want to know when a button is pressed while that child form is loaded. I have this code: private void frmTestBaby_KeyUp(object...
3
by: Lachlan Hunt | last post by:
Hi, I've been looking up lots of documentation and trying to work out a cross-platform method of capturing a keyboard event and working out which key was pressed. From what I can find, there...
1
by: Ori | last post by:
Hi Guys, Here is my problem, but maybe someone can help me with this. Background: 1. Using C#. 2. I'm having a form which the KeyPreview is on (and must stay like this) and I'm...
7
by: Coder | last post by:
Hi I have the following code in java script, it is not giving proper output in FIREFOX but running fine in IE... can anybody help me out to make this run in FIREFOX . <script...
1
by: Frank O'Hara | last post by:
I think I'm losing my mind, granted it is kind of late here so... I'm trying to do some simple validation on the client as keys are pressed. The validation routine works well enough however I...
3
by: Academia | last post by:
If the user types into the TextBox of a combobox it works OK. But if he hold a key down so the letter repeats quickly the code runs into trouble. My questions are: Is it true that an event's...
3
by: Brent | last post by:
The code below correctly sets an iframe to be editable, and then tries to attach an "onkeyup" event to the iframe. Instead of doing that, however, it generates a cryptic "Object required" error in...
1
by: Chris Jobson | last post by:
>I have a canvas which I use for dragging shapes around such as rectangles Add Focusable="True" to the XAML for the PolyLine and Rectangle elements and then when you set the focus to them they...
2
by: Tony Johansson | last post by:
Hello! I have created a Control that consist of a label and a textbox.I have called this class ctlLabelTextbox. public partial class ctlLabelTextbox : UserControl { .... } The class that I...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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.